views:

500

answers:

2

Hi everybody,

In my javascript application(using prototype), I need some info from a third party server an this server sometimes requires web interaction with the user, and for that reason it sends 302 http code with a new URL in the Location header. What I want is to capture this new URL to open it in a separate window, however the method getHeader('Location') is always returning null. Any Idea??? This is simplified version of my code:

UPDATE = function(){
new Ajax.Request(proxy_url,{
 method: 'post', 
 parameters: "p1=1&p2=2",
 on302: function(response){
  OpenURLfromLocation(response);
 },

 onSuccess: function(transport){
  alert("OK");
 }

});}
OpenURLfromLocation = function(response){
alert(response.getHeader('Location'));
}

The ajax proxy is working properly and I can see on firebug that it behaves correctly until it tries to recover the location from the header.

+1  A: 

Most browsers consider the request of a third-party url from ajax (XMLHttpRequest) a security violation. http://www.xml.com/pub/a/2005/11/09/fixing-ajax-xmlhttprequest-considered-harmful.html

You should probably consider requesting this through a proxy or other means (like curl in php) which make the javascript request a local one.

Not sure, if that 100% answers your question, but probably should also be considered in your case here.

txwikinger
I already have a proxy that deals with that issue and it maps both content and headers and still having the issue. Thanks anyway
4NDR01D3
Could it be a problem of scope? Try the alert from OpenURLfromLocation inside the Ajax request, or make the function named likefunction OpenURLfromLocation(response){alert(response.getHeader('Location'));}
txwikinger
I did it... not success =(
4NDR01D3
A: 

It's been a while since I've used Prototype, but on the actual XMLHttpRequest object, the method name is getResponseHeader. However, if you control the server's response, why not just return the new URL in the response body?

As a side note, if you're adding "location" to the response header being sent back from the server, the convention is to prepend anything that you add to the headers with an "X-", so you would have "X-Location"

Justin Johnson
Thanks... yep.. i have tough in send that as a parameter, is just that doesnt sound to me as the right way to do it. I think should be possible from javascript.Thanks for the tip abot the "X-" headers.prototype just mask that method to return a null instead of throwing an exception as the original method does.
4NDR01D3
Sending it in the header seems like an odd thing to me. Response data belongs in the body of the response, the header is for extra/meta information. So, if it the location is the main data of the response, it belongs in the body. If the location is an addition to a "normal" response that contains other data in the body, then I suppose that being passed as a header make sense in that scenario.
Justin Johnson
mhh.. maybe you are right, I will go for that solution, but still funny that firebug can extract that header, but i cant from prototype =(
4NDR01D3
Ya it is strange, but like I said, it's been a very long time since I've used Prototype (back in 2004), so I'm not really that familiar with it anymore and can't help with the fine details there. Anyway, mark this as the solution so we know this question is solved ;)
Justin Johnson