Hybrid View

  1. #1
    Sencha User
    Join Date
    Jul 2007
    Location
    London
    Posts
    25
    Vote Rating
    1
    mattbennett is on a distinguished road

      0  

    Default Following HTTP Redirects (302)

    Hi all,

    I'm trying to figure out how to recognize when an HTTP redirect comes back from an Ajax request. Here is my submission code:

    Code:
    Ext.Ajax.request({
    	url: base.url+'/export/foo',
    	method:'GET',
    	success:function(response) {
    		console.log(response)
    	}, 
    	failure:function(response) {
    		debug.popup(response.responseText);	//show the server response in a dialog
    	}
    })
    On the serverside, a request to "whatever.com/export/foo'" generates a file and then returns an HTTP redirect to that file. Without Ajax, the browser follows the redirect and gives me an "open/save" dialog as you'd expect.

    With Ajax, I was expecting the success function above to output an object with status=302 and a redirect URL which I could push to location.href. Actually what I get in the console is "Object tId=8 status=200 statusText=OK". Firebug also shows me that the browser does honor the redirect and actually downloads the file, but I get no "open/save" dialog.

    What's the correct way of dealing with this situation? I really want my serverside code to remain the same for both Ajax and non-Ajax operation.

    Many thanks,
    Matt.

  2. #2
    Ext User halkon_polako's Avatar
    Join Date
    Nov 2007
    Location
    Barcelona
    Posts
    193
    Vote Rating
    0
    halkon_polako is on a distinguished road

      0  

    Default

    If you have in the console a status=200, is because the response from the server had been a http-200.

    A response with http-302 is a failure.
    Ext.Ajax only call success callback on http status >=200 and <300.

  3. #3
    Sencha User
    Join Date
    Jul 2007
    Location
    London
    Posts
    25
    Vote Rating
    1
    mattbennett is on a distinguished road

      0  

    Default

    Hi Halkon,

    Thanks for your reply. From your input, my guess is that the 200 response I'm seeing in the success function is generated *after* the redirect (i.e. the browser is following the 302, downloading the file successfully and reporting a 200 status). This also explains why FireBug crashes when I try to examine the response object - the file it's downloading is ~5MB.

    So, I guess that changes my question slightly: is there any way to intercept the 302 redirect and act on it? Obviously I don't want to be downloading the file via Ajax; instead, I should capture the redirect URL and push it to location.href to download the file normally.

    Thanks,
    Matt.

  4. #4
    Ext User halkon_polako's Avatar
    Join Date
    Nov 2007
    Location
    Barcelona
    Posts
    193
    Vote Rating
    0
    halkon_polako is on a distinguished road

      0  

    Default

    You can try the hendricd's extension ext-basex.js (http://extjs.com/forum/showthread.php?t=21681) where you can intercept http status codes and assign a function to them...

    But...are you sure that the browser is catching the 302-redirect and then 'redirecting' to the new location? Have you seen the 302 code?... maybe you are receiving directly the file without a redirect...

  5. #5
    Sencha User
    Join Date
    Jul 2007
    Location
    London
    Posts
    25
    Vote Rating
    1
    mattbennett is on a distinguished road

      0  

    Default

    Thanks Halkon,

    I'll give ext-basex a whirl - looks like it'll do everything I need.

    Yes, upon closer inspection the browser definitely sees the 302; the Net tab on FireBug shows this:

    Code:
    23 (302)
    http://whatever.com/export/foo?_dc=1206382633661
    whatever.com	
    0 b
    	
    47ms
    foo.zip
    whatever.com/media/generated/foo.zip
    whatever.com
    4.76 MB
    Thanks for the excellent responses. Hopefully this thread will explain the behavior of 300 responses for some other folks too.

    Matt.

  6. #6
    Ext User
    Join Date
    Jun 2008
    Location
    SF Bay Area,CA
    Posts
    40
    Vote Rating
    0
    coderobo is on a distinguished road

      0  

    Default

    I was tempted by this thread to look at using basex for trapping redirect. Unfortunately basex can't handle 302 as the browser preempts it. See posts in another thread http://extjs.com/forum/showthread.php?t=21681&page=11

    Bottom line - redirects considered harmful with ExtJS and other AJAX frameworks.