Spring MVC-使用jQuery重定向 [英] Spring MVC - redirect with jQuery
问题描述
我正在对控制器进行AJAX调用,我想重定向到成功页面上的新页面.因此,我的成功回调函数中包含重定向逻辑.但是它不会重定向到新页面,而是停留在同一页面上.
I am making an AJAX call to a controller, and I want to redirect to a new page on Success. So I have have the redirection logic within my success callback function. But it doesn't redirect to a new page instead it stays on the same page.
The intriguing thing is that a GET Request (with the form Data I just sent via POST) is made after the Ajax Callback is executed. that is, I see a GET Request in the Address Bar.
这是我的AJAX请求
$.ajax({
url: "processData",
type: "POST",
dataType: 'json',
contentType:'application/json',
async: false,
data: JSON.stringify(req),
success: function(result) {
url = window.location.href;
url = url.replace("processData", "getMoreData");
alert(url); // correct url is printed
window.location.replace(url);
},
error: function() {
alert("--- Failure ---");
}
});
这是怎么了?
推荐答案
上面@Chlebik发布的答案是正确的.我面临的主要问题是both GET & POST requests were getting generated
.因此,有时GET请求正在进行中& POST请求已被取消.在其他时候,情况正在发生逆转&我得到了正确的结果.
The answer posted by @Chlebik above is correct. The main issue I was facing was that both GET & POST requests were getting generated
. So sometimes the GET request was going forward & the POST request was getting cancelled. Other times the reverse was happening & I was getting correct result.
我要做的就是避免生成默认表单的提交"按钮中的GET请求.
All I had to do was to avoid the GET request, from the default form's Submit button, from getting generated.
这是代码:
$('form').on('submit', function(e) {
e.preventDefault();
我已经使用了代码fhttp://stackoverflow.com/questions/32953573/form-processing-via-ajax-avoiding-both-get-post-requests-from-getting-genera
I have taken the code fhttp://stackoverflow.com/questions/32953573/form-processing-via-ajax-avoiding-both-get-post-requests-from-getting-genera
这篇关于Spring MVC-使用jQuery重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!