我正在使用 ajax 表单 jquery 插件通过 ajax 提交表单(在对话框中)。
这工作正常,然后我从服务器返回 html 响应。响应来 self 无法修改的标准发布后重定向 php 页面。
有没有办法使用 jquery(在 ajax 回调中)获取此重定向的 url(最终 GET 位置)?
$j('span.sfAutocomplete a').click(function(e){
var url = this.href;
var $dialog = $j('<div id="ajaxDialog"></div>').appendTo('body')
.load(
url,
'sfAutocomplete=true',
function (responseText, textStatus, XMLHttpRequest) {
$dialog.dialog({ autoOpen: true });
//
// Ajax submit
//
$j('#ajaxDialog form').submit(function() {
function showResponse(responseText, statusText) {
// how to get the redirect url ?
}
$j(this).ajaxSubmit({
success: showResponse
});
return false;
});
}
);
return false;
});
最佳答案
我没用过你用的插件,但是如果你用jQuery Ajax command ,您收到 XMLHttpRequest 对象作为 complete
事件的参数。然后,您可以从返回的 HTTP header 中获取发布 URL。请尝试以下操作:
$.ajax({
url:'your.url',
data:'your data',
complete: function(xhr,textstatus) {
// xhr.responseText contains the response from the server
var allheaders = xhr.getAllResponseHeaders();
// this will get all headers as a string - if you want them as an object...
var eachheader = allheaders.split('\n');
var headers = {};
for(i = 0; i < eachheader.length; i++) {
if ($.trim(eachheader[i]) !== '') {
headersplit = eachheader[i].split(':');
headers[headersplit[0]]=$.trim(headersplit[1]);
}
}
}
});
此代码复制自 this thread .
关于javascript - jquery ajax 表单 - 如何获取重定向 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1666205/