views:

523

answers:

1

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;
  });
+2  A: 

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('\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.

Traveling Tech Guy
i've tried with the complete handler + xhr.getResponseHeader or getAllResponseHeaders and it returns 'undefined'. if i do console.log( xhr ) it prints: Object aborted=0 (even if the connetion goes well)
gpilotino
Strange. What does the xhr object contain then?
Traveling Tech Guy
it looks like it was a "bug" of the ajax form plugin when used with the "iframe" option (it uses an xhr mock object). once get rid of that option now i get the correct http headers but the "Location" header is missing so I still don't get the URL.
gpilotino
Is there anything special about the page that returns from the post operation? Does t actively manipulate the HTTP headers? (i.e., adding a cookie, changing the content-type, etc.?)
Traveling Tech Guy
actually, it doesn't
gpilotino