Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取使用jquery.ajax()重定向的帖子后的实际URL_Jquery_Ajax_Url_Post_Redirect - Fatal编程技术网

获取使用jquery.ajax()重定向的帖子后的实际URL

获取使用jquery.ajax()重定向的帖子后的实际URL,jquery,ajax,url,post,redirect,Jquery,Ajax,Url,Post,Redirect,我在我的web应用程序中遵循PRG(Post Redirect Get)模式,并在我的大多数帖子中使用如下内容: $.ajax({ type: 'POST', url: 'A.html', data: '....', statusCode: { 302: function() { alert("302"); // this is never called }, 200: function()

我在我的web应用程序中遵循PRG(Post Redirect Get)模式,并在我的大多数帖子中使用如下内容:

$.ajax({
    type: 'POST',
    url: 'A.html',
    data: '....',
    statusCode: {
        302: function() {
            alert("302"); // this is never called
        },
        200: function() {
            alert("200");
        },
    },
    success: function (data, textstatus) {
        alert('You are now at URL: ' + ??);
    },
    error: function (data) {
    },
    complete: function (jqXHR, textstatus) {
        alert('You are now at URL: ' + ??);
    },
});
我需要在任何重定向发生后获取URL,即.ajax()函数调用的最终get的URL。例如,到a.html的帖子可以重定向到B.html或C.html(始终通过302)。如何获取最终URL


我使用的是jQuery1.5.1,使用代理可以看到jquery在默默地跟踪重定向,我对此很满意。我不关心任何响应302的URL—我只想知道.ajax()的“成功:”或“完成:”钩子被触发时的最终请求的URL。

首先尝试找出重定向返回的状态代码(通常大于300)

需要注意的另一点是,执行顺序:
成功\失败-->状态码函数-->完成

我最终解决了这个问题,在我的所有回复中添加了一个额外的标题(例如“X-MYAPP-PATH:/Admin/Index”)

因此,我的javascript可以更改为以下内容:

success: function (data, textstatus, xhrreq) {
    alert('You are now at URL: ' + xhrreq.getResponseHeader("X-MYAPP-PATH"));
},

<>我仍然相信jQuery应该能够给我当前的URL,所以我认为这是一个HACK。

< P>一个更好的解决方案是给jQuery的Ajax提供一个定制的XHR对象,比如:

var xhr = new XMLHttpRequest();

$.ajax({
    url: '/url',
    type: 'post',
    data: '...',
    xhr: function() {
         return xhr;
    }
});
然后,您可以在任何回调中访问当前URL

success: function () {
    alert('You are now at URL: ' + xhr.responseURL);
}

发送带有数据的url并在Ajax的
success
部分中检索。您可以用示例详细说明,并请指定是否发生嵌套重定向。用这个例子详细说明一下——例如,一篇到a.html的文章可能会重定向到B.html或C.html(总是通过302)。如何获取最终的URL?我已经使用代理确认,发送到a.html的帖子将以“302 Found”和位置B.html响应。然而,问题的关键是,我无法查询javascript/jquery来获得任何证据,证明我被重定向到了B.html的任何地方。若我问$(this.url),我会得到“A.html”,window.location等也一样。Nope-jquery隐藏了302的任何证据。xhreq.status是200,xhr.getHeader()不是可用的函数。@Tom,getHeader工作正常,但我需要检查302(也许你是对的,你可以执行
xhreq.getAllResponseHeaders()
,但是如果
位置不在那里,你就不走运了。@TomWells是正确的,无论如何你都不会得到302响应。重定向被浏览器隐藏。
success: function () {
    alert('You are now at URL: ' + xhr.responseURL);
}