jQuery Ajax和服务器重定向响应 [英] jQuery Ajax and redirect response from server

查看:219
本文介绍了jQuery Ajax和服务器重定向响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有什么问题吗?我该如何处理这种情况?

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

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

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

这是代码的样子:

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;
}

单击页面上的按钮时,将运行上述功能.但是这个页面继续显示.浏览器调试器显示它确实从应该用来刷新页面的新操作中收到200 OK响应.我正在使用Chrome浏览器和jquery 1.6.1

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

对不起,我在代码示例中的错字.我纠正了.

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

推荐答案

服务器无法从ajax请求进行重定向.最后,ajax涉及到客户端(浏览器).如果您想重定向,则可以执行此操作,但是必须在客户端的回调中完成.您可以通过从服务器返回一个包含要重定向到的URL的对象来做到这一点-然后,您可以使用JavaScript更改文档的location属性.我认为,如果您未在所有情况下都进行重定向,或者服务器端调用是一个长期运行的过程,这将很有意义.如果不是这种情况,那么ajax调用首先可能没有任何意义.

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.

这篇关于jQuery Ajax和服务器重定向响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆