PDA

View Full Version : Ext.Ajax automatically redirects when return code is 302 and there is a Location head



training_desso
23 Jan 2013, 2:32 PM
In this example I am making ajax request. It returns a response 302 (redirect) and Location header set to something I want to access. I also need to access the rest of the response headers, unfortunately the response I am getting contains not my original response, but a second one automatically done with a URL set in Location header. This is somewhat logical, but I do not want this to happen automatically, I want to handle it myself...


Ext.Ajax.request({
url: url,
params: {},
callback: function(options, success, response){

// response here contains response info for the second request doe automatically based on
// the location header

},
scope: this
});

scottmartin
24 Jan 2013, 9:44 AM
What values are you getting back in your callback parameters?

What do you see using:



Ext.Ajax.on('beforerequest', function(conn, response, options) {
console.log(response);
});

Ext.Ajax.on('requestcomplete', function(conn, response, options) {
if (response.responseText && response.status === 0) {
response.status = response.text ? 200 : 404;
}
console.log(response);
});

Ext.Ajax.on('requestexception', function(conn, response, options) {
console.log(response);
});


Scott.

training_desso
25 Jan 2013, 12:51 PM
Hi Scott,

Thanks for your reply. I've changed my post... the issue seems to happen with 302(redirect) responses. It automatically redirects and I get failure, because the URL it redirects is not valid...

training_desso
25 Jan 2013, 12:52 PM
It there a way to tell Ext.Ajax to not automatically redirect when the response is 302 with Location header?

training_desso
25 Jan 2013, 2:28 PM
http://stackoverflow.com/questions/7524039/problems-with-xmlhttprequest-status-302

Looks like Browser's XMLHttpRequest is doing the redirect. There is a workaround to tell it to not redirect by setting a variable to 0. Does ExtJs expose it?

skirtle
25 Jan 2013, 4:10 PM
Indeed, the redirect is performed by the browser, not ExtJS.

I suspect the workaround you mentioned won't work cross-browser, a quick bit of searching suggests that the channel property is only available in Gecko.

If you only care about Firefox then you could apply the workaround with a small amount of tweaking. Personally I'd probably override openRequest (a method Ext.Ajax inherits from Ext.data.Connection).