使用javascript原型获取ajax调用的位置标头

时间:2022-10-08 08:57:45

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:

在我的javascript应用程序(使用原型)中,我需要来自第三方服务器的一些信息,该服务器有时需要与用户进行Web交互,因此它在Location头中发送带有新URL的302 http代码。我想要的是捕获这个新URL以在单独的窗口中打开它,但方法getHeader('Location')总是返回null。任何的想法???这是我的代码的简化版本:

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.

ajax代理工作正常,我可以在firebug上看到它的行为正确,直到它尝试从标头恢复位置。

2 个解决方案

#1


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?

我使用Prototype已经有一段时间了,但在实际的XMLHttpRequest对象上,方法名是getResponseHeader。但是,如果您控制服务器的响应,为什么不在响应正文中返回新的URL?

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"

作为旁注,如果您要将“位置”添加到从服务器发回的响应标头中,那么约定是将您添加到标题中的任何内容添加到“X-”,因此您将拥有“X-”地点”

#2


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

大多数浏览器认为来自ajax(XMLHttpRequest)的第三方URL的请求是安全违规。 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.

您可能应该考虑通过代理或其他方式(如php中的curl)请求此操作,这使得javascript请求成为本地请求。

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

不确定,如果100%回答你的问题,但在这种情况下也可能应该考虑你的问题。

#1


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?

我使用Prototype已经有一段时间了,但在实际的XMLHttpRequest对象上,方法名是getResponseHeader。但是,如果您控制服务器的响应,为什么不在响应正文中返回新的URL?

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"

作为旁注,如果您要将“位置”添加到从服务器发回的响应标头中,那么约定是将您添加到标题中的任何内容添加到“X-”,因此您将拥有“X-”地点”

#2


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

大多数浏览器认为来自ajax(XMLHttpRequest)的第三方URL的请求是安全违规。 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.

您可能应该考虑通过代理或其他方式(如php中的curl)请求此操作,这使得javascript请求成为本地请求。

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

不确定,如果100%回答你的问题,但在这种情况下也可能应该考虑你的问题。