Enrique Moreno Tent
Enrique Moreno Tent

Reputation: 25307

Updating URL address after AJAX call to a 302

After I do an AJAX request with jQuery, I want to update the URL, with the following code

history.pushState(object, title, new_url)

So I wrote the following code

$.ajax({
    url: url,
    success: onSuccess
})

var onSuccess = function() {
    ...
    history.pushState(object, title, new_url)
}

The problem is that the URL where I go make the AJAX call returns a 302 code, so I need a way to find out where is the final URL.

Is there a way to find this information?

Upvotes: 0

Views: 247

Answers (1)

Kresimir Pendic
Kresimir Pendic

Reputation: 3614

can you see if this works in your case ..

$.ajax({
    url: url,
    success: function(data, textStatus, xhr),
})

var onSuccess = function(data, xhr, textStatus) {
    ...
    if( xhr.status == 200 ) history.pushState(object, title, new_url)
}

EDIT:

I have tested with my live site and I console.log'ed final dest URL successfully with this:

(function($){

var xhr;
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
  xhr = _orgAjax();
  return xhr;
};


$.ajax({
    url: "https://vizkultura.hr/dizajn",
    success: function(data) {
        console.log( xhr.responseURL );
    },
});

})(jQuery)

outputed new url: https://vizkultura.hr/dizajn-07-12/

try it @your end and see if it fits, cheers, k

Upvotes: 2

Related Questions