简体   繁体   English

通过修改HTTP标头在不使用CORS的情况下使用XMLHttpRequest?

[英]Using XMLHttpRequest without CORS by modifying HTTP headers?

I'm doing some tests with the (deprecated) Twitter API 1.0 我正在使用(不建议使用的)Twitter API 1.0做一些测试

For example, I want to get data from the API, client-side using AJAX browser requests from any cross-origin webpage. 例如,我想使用来自任何跨域网页的AJAX浏览器请求从API客户端获取数据。 It can be a new blank tab, a local HTML page or any existing website. 它可以是新的空白选项卡,本地HTML页面或任何现有网站。

I've tried JSONP, it works great but I would like to use the default XMLHttpRequest even if Twitter servers do not support CORS http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing . 我已经尝试过JSONP,它工作得很好,但是即使Twitter服务器不支持CORS http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing,我也想使用默认的XMLHttpRequest。

On google.com homepage for example, I create a simple AJAX call to Twitter API that I execute with Firebug: 例如,在google.com主页上,我创建了一个用Firebug执行的对Twitter API的简单AJAX调用:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.twitter.com/1/friends/ids.json?screen_name=baptx", false);
xhr.send();

This will not work and print an error on Firebug due to the same origin policy: 由于相同的原始政策,这将无法正常工作并在Firebug上显示错误:

Error: Failure
xhr.send();

It returns an HTTP 200 OK code but no JSON data has been received from the server. 它返回HTTP 200 OK代码,但未从服务器接收到JSON数据。

I've seen two differences between a request from a google.com webpage and the api.twitter webpage (who works for Twitter API requests since it's the API domain name, same origin). 我已经看到了来自google.com网页的请求和api.twitter网页(谁是Twitter API请求的用户,因为它是API域名,起源相同)之间的两个区别。

An Origin HTTP header has been added with the current domain name: 原始HTTP标头已添加了当前域名:

Origin  https://www.google.com

The Referer HTTP header is not https://api.twitter.com/ like a request from api.twitter.com page but is in my case: Referer HTTP标头不像api.twitter.com页面上的请求那样是https://api.twitter.com/,但在我的情况下:

Referer https://www.google.com/webhp?hl=en

That's why I've tried to remove the Origin HTTP header and modify the current Referer HTTP header to https://api.twitter.com/ 这就是为什么我尝试删除Origin HTTP标头并将当前Referer HTTP标头修改为https://api.twitter.com/的原因

I've done this with the Firefox ModifyHeaders extension and it works, I can check in Firebug "Net" tab that those changes were made correctly. 我已经完成了Firefox ModifyHeaders扩展程序的工作,并且可以正常工作,我可以在Firebug的“ Net”选项卡中检查是否正确进行了这些更改。

Now, I have the SAME request header from a request coming from google.com webpage and api.twitter.com webpage. 现在,我有来自google.com网页和api.twitter.com网页的请求的SAME请求标头。 It will still fail to do an AJAX request from another domain than the API, even if the HTTP headers are overwritten, why? 即使HTTP标头被覆盖,它仍然无法从API之外的其他域发出AJAX请求,为什么?

By the way, do you know why an AJAX request to Twitter API from Firefox "New Tab" will work? 顺便说一句,您知道为什么Firefox“新标签页”对Twitter API的AJAX请求将起作用吗?

If web servers don't allow Cross-origin resource sharing, we have to manually add the HTTP response header Access-Control-Allow-Origin: * 如果Web服务器不允许跨域资源共享,则必须手动添加HTTP响应标头Access-Control-Allow-Origin:*

I thought the problem was in request headers. 我认为问题出在请求标头中。 There was no Firefox addon to modify HTTP response headers, only request headers are supported by ModifyHeaders or TamperData: Modifying HTTP response headers in Firefox 没有Firefox插件可以修改HTTP响应标头,ModifyHeaders或TamperData仅支持请求标头: 在Firefox中修改HTTP响应标头

My question was in fact similar to this one: Can I disable SOP (Same Origin Policy) on any browser for development? 我的问题实际上与此相似: 我可以在任何用于开发的浏览器上禁用SOP(相同来源策略)吗?

Solutions: Someone has developped a Firefox addon to force CORS: https://addons.mozilla.org/en-US/firefox/addon/forcecors/ . 解决方案:有人开发了一个用于强制CORS的Firefox插件: https : //addons.mozilla.org/en-US/firefox/addon/forcecors/ Or we can use GM_xmlhttpRequest in a GreaseMonkey script, it will bypass the same origin policy of XMLHttpRequest. 或者我们可以在GreaseMonkey脚本中使用GM_xmlhttpRequest,它将绕过XMLHttpRequest的相同原始策略。 In Chrome, there is no addon to modify HTTP request/response headers like you want since the browser does not provides APIs, but there is a flag to disable SOP (--disable-web-security) 在Chrome中,由于浏览器不提供API,所以没有插件可以随意修改HTTP请求/响应标头,但是有一个标志可以禁用SOP(--disable-web-security)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM