最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery ajax form - how to get the redirect url? - Stack Overflow

programmeradmin1浏览0评论

i'm using ajax form jquery plugin to submit a form (in a dialog) via ajax.

this works fine and then i get the html response back from the server. the response es from a standard redirect-after-post php page which i cannot modify.

is there a way to obtain the url of this redirect (the final GET location) using jquery (inside the ajax callback) ?

  $j('span.sfAutoplete a').click(function(e){
    var url = this.href;
    var $dialog = $j('<div id="ajaxDialog"></div>').appendTo('body')
    .load(
      url,
      'sfAutoplete=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'm using ajax form jquery plugin to submit a form (in a dialog) via ajax.

this works fine and then i get the html response back from the server. the response es from a standard redirect-after-post php page which i cannot modify.

is there a way to obtain the url of this redirect (the final GET location) using jquery (inside the ajax callback) ?

  $j('span.sfAutoplete a').click(function(e){
    var url = this.href;
    var $dialog = $j('<div id="ajaxDialog"></div>').appendTo('body')
    .load(
      url,
      'sfAutoplete=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;
  });
Share Improve this question asked Nov 3, 2009 at 9:25 gpilotinogpilotino 13.3k9 gold badges50 silver badges62 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I haven't used the plug-in you are using, but if you use the jQuery Ajax mand, you receive the XMLHttpRequest object as a parameter to the plete event. You can then get the post URL from the HTTP header that returns. Try the following:

$.ajax({
  url:'your.url',
  data:'your data',
  plete: 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.

发布评论

评论列表(0)

  1. 暂无评论