简体   繁体   English

简单的XML HttpRequest问题

[英]Simple XML HttpRequest question

I'm trying to launch a request for some data from an external website w/ an API. 我正在尝试从带有API的外部网站启动一些数据请求。

If I copy paste the request into my web-browser it works fine. 如果我将请求复制粘贴到我的网络浏览器中,则可以正常工作。 Example such as (http://example.com/json/user/search/all) I can see the results in the browser window. 例如(http://example.com/json/user/search/all)的示例,我可以在浏览器窗口中看到结果。

However, I'm trying to launch this query from a website and I'm running into an issue: Using some javascript like this: var req = new XMLHttpRequest(); 但是,我试图从一个网站启动此查询,但遇到一个问题:使用如下所示的javascript:var req = new XMLHttpRequest(); req.open('GET', abovementionedurl, true) req.send() req.open('GET',上述URL,true)req.send()

I get an error that reads: Origin (MyDOMAIN) is not allowed by Access Control Allow Origin. 我收到一条错误消息:Access Control允许原点不允许原点(MyDOMAIN)。

Again, I'm fairly new to XMLHttpRequests and I think this is fairly simple to solve. 同样,我对XMLHttpRequests还是很陌生,我认为这很容易解决。 Anyone know? 有人知道吗 (I tried searching google but couldn't find a good answer) Thanks in advance. (我尝试搜索google,但找不到合适的答案)预先感谢。

The problem is the same-origin policy . 问题是同源政策 This is a rule that XMLHTTPRequests may not be used except on the same domain as the original page. 这是一条规则,除了在与原始页面相同的域上, 不得使用XMLHTTPRequests。 This is for security reasons. 这是出于安全原因。

The easiest way around it is to write a server-side script that proxies the request for you. 解决该问题的最简单方法是编写一个服务器端脚本来代理您的请求。

You are not allowed to do Cross-Site XMLHttpRequests and need to use a proxy to accomplish it. 不允许您执行跨站点XMLHttpRequest,并且需要使用代理来完成它。

This article from Yahoo explains it in detail and gives more ideas of how to solve it. 雅虎的这篇文章详细解释了它,并给出了更多解决方案的想法。 But basically it should not be done because this restriction exists for security purposes. 但基本上不应该这样做,因为出于安全目的存在此限制。

It would be better if you used PHP and fopen() for instance and simply called that page using your XMLHttpRequest object. 最好使用PHP和fopen()例如,并使用XMLHttpRequest对象简单地调用该页面。

This is your browser's protection against cross side scripting. 这是您的浏览器针对跨脚本的保护。 You are not allowed to access other pages that come from a different domain. 您无权访问其他域中的其他页面。

EDIT: Also check this: Cross-site XMLHttpRequest 编辑:还要检查此: 跨站点XMLHttpRequest

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

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