Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

javascript - 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.

this works fine and then i get the html response back from the server. the response comes 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.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;
  });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. You can then get the post URL from the HTTP header that returns. 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('
');
    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...