javascript jQuery Ajax 和来自服务器的重定向响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6611249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:16:55  来源:igfitidea点击:

jQuery Ajax and redirect response from server

javascriptjqueryajax

提问by rjc

I have a situation where I send jquery ajax post request but in my web application, the handler for this specific ajax request (after processing post parameters), will invoke an action in another controller ( not sure if this is called redirect) which basically renders the whole website page ( like page refresh). But I notice browser keeps display the same page instead of refreshing to the content of the new page.

我有一种情况,我发送 jquery ajax post 请求,但在我的 web 应用程序中,此特定 ajax 请求的处理程序(在处理 post 参数之后)将调用另一个控制器中的操作(不确定这是否称为重定向),它基本上呈现整个网站页面(如页面刷新)。但是我注意到浏览器一直显示相同的页面,而不是刷新到新页面的内容。

Is there something wrong? How can I deal with this situation?

有什么不对?我该如何处理这种情况?

I had to edit my question because I changed my ajax call.

我不得不编辑我的问题,因为我改变了我的 ajax 调用。

This is what the code looks like:

这是代码的样子:

function chkSubmit(event, actionType) {


        var msgid = showlst('Please wait ...');
        var data = ''
        if (actionType == 'IAmDone') {
            var letters = 'e,b,c'

            data = 'actionType=' + actionType + '&letters=' + letters;

        } else data = 'actionType=' + actionType;
        $j.ajax({
            type: 'POST',       
            url: context + '/app/handleChk',
            data:  data
        });

        return false;
}

The above function runs when a button on the page is clicked. But this same page keep displaying. The browser debugger shows that it did receive 200 OK response from the new action which it was supposed to refresh page with. I am using Chrome browser and jquery 1.6.1

当点击页面上的按钮时,上面的函数运行。但是这个页面一直在显示。浏览器调试器显示它确实收到了来自它应该用来刷新页面的新操作的 200 OK 响应。我正在使用 Chrome 浏览器和 jquery 1.6.1

Sorry for my typo in code sample. I corrected it.

抱歉,我在代码示例中打错了字。我纠正了它。

回答by ek_ny

The server can not do a redirect from an ajax request. In the end ajax involves the client (browser). If you want to redirect you can do it, but it is going to have to be done on client side, in the callback. You can do that by returning an object from the server which contains the url you want to redirect to--- you can then you javascript to change the document's location property. I would think that this would make sense if you were not redirecting in all cases, or if your server side call was a long running process. If neither is the case then an ajax call probably doesn't make sense in the first place.

服务器无法从 ajax 请求进行重定向。最后ajax涉及到客户端(浏览器)。如果你想重定向,你可以这样做,但它必须在客户端的回调中完成。您可以通过从服务器返回一个包含您要重定向到的 url 的对象来实现这一点——然后您可以使用 javascript 来更改文档的位置属性。如果您不是在所有情况下都进行重定向,或者您的服务器端调用是一个长时间运行的过程,我认为这会有意义。如果两者都不是,那么 ajax 调用可能一开始就没有意义。

回答by picus

I may be misreading your question, but where is your success callback function in that ajax call? That is where you would typically render the results into the view, also, you could use the error callback to get some data on what, if anything is going wrong:

我可能误读了您的问题,但是在那个 ajax 调用中您的成功回调函数在哪里?这是您通常将结果呈现到视图中的地方,此外,如果出现任何问题,您可以使用错误回调来获取一些数据:

function chkSubmit(event, actionType) {


        var msgid = showlst('Please wait ...');
        var actionType = type // per j. tuskan - looks like no such var in scope
        var data = ''
        if (actionType == 'IAmDone') {
            var letters = 'e,b,c'

            data = 'actionType=' + actionType + '&letters=' + letters;

        } else data = 'actionType=' + actionType;
        $j.ajax({
            type: 'POST',       
            url: context + '/app/handleChk',
            data:  data,
            success:function(the_data){
              alert("Now I can do stuff with the ajax response which is: "+the_data);
            }
        });

        return false;
}

回答by Thomas

Example of what has been said by @ek_ny.

@ek_ny 所说内容的示例。

jQuery.ajaxSetup({
    complete: function (request, textStatus) {
        // header inserted manually on the server.
        // you should block the automatic redirect headers 
        // inserted by the server.
        var location = request.getResponseHeader("Location");
        if(location) window.location = location; 
    }
});

回答by Ganesh

You can not do server side redirection using Ajax because whenever you use Ajax, response will come to success/error function of Ajax. You can do server side redirection using 'form' in JavaScript.

您不能使用 Ajax 进行服务器端重定向,因为无论何时使用 Ajax,都会响应 Ajax 的成功/错误功能。您可以使用 JavaScript 中的“表单”进行服务器端重定向。