与Ajax和Spring在同一页面中重定向新URL



我有一些代码ajax

    $.ajax({
        a :a,
        b : b,
        type : 'GET',
        method: 'GET',
        url   : "/report-join-promo/list",
        data : {a,b}
    });

我的控制器中有这个脚本

@RequestMapping(value = "/list", method = RequestMethod.GET )
public String ReportJoinPromoList(@RequestParam(value = "a" ,required = false,defaultValue = "") String a ,
                                  @RequestParam(value = "b" ,required = false,defaultValue = "") String b ,
                                  Model model){
    return "pages/promo/report";
}

第一个URL是http://localhost:8080/report/list我希望我的URL成为http://localhost:8080/report/list?a = valueofa& b = valueofb在同一页面中,但我无法解决此

如果您只能使用HTML5,则可以使用history API更改URL,而无需进入服务器。因此,您的代码现在看起来像:

var a = 'Param1';
var b = 'Param2';
$.ajax({
    url : "/report-join-promo/list",
    type : 'GET',
    data : {
        a: a,
        b: b
    },
    success: function(response) {
         // Your code
    },
    error: function(xhr) {
        // Your code
    }
});
if(history.pushState) {
    history.pushState({}, 'Page Title', 'list?a=' + encodeURIComponent(a) + '&b=' + encodeURIComponent(b));
}

在Aspsnippets上演示了此API的一个很好的例子。有关更多详细信息,请阅读MDN DOC

最新更新