简体   繁体   English

jquery ajax表单 - 如何获取重定向url?

[英]jquery ajax form - how to get the redirect url?

i'm using ajax form jquery plugin to submit a form (in a dialog) via ajax. 我正在使用ajax表单jquery插件通过ajax提交表单(在对话框中)。

this works fine and then i get the html response back from the server. 这工作正常,然后我从服务器返回html响应。 the response comes from a standard redirect-after-post php page which i cannot modify. 响应来自一个标准的重定向后发布的php页面,我无法修改。

is there a way to obtain the url of this redirect (the final GET location) using jquery (inside the ajax callback) ? 有没有办法使用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;
  });

I haven't used the plug-in you are using, but if you use the jQuery Ajax command , you receive the XMLHttpRequest object as a parameter to the complete event. 我没有使用您正在使用的插件,但是如果您使用jQuery Ajax命令 ,则会收到XMLHttpRequest对象作为complete事件的参数。 You can then get the post URL from the HTTP header that returns. 然后,您可以从返回的HTTP标头中获取帖子URL。 Try the following: 请尝试以下方法:

$.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 code was copied from this thread . 此代码是从此线程复制的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM