xmlhttprequest 跨站域问题? [英] Xmlhttprequest Cross site domain issue?

查看:28
本文介绍了xmlhttprequest 跨站域问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被一个问题困住了一段时间,但还没有得到最好的解决方案.我正在从驻留在服务器上的 javascript 调用 xmlhttprequest (POST) 说http://localhost:8080"到另一台服务器http://localhost:9090"

出现浏览器错误 如下所述:异常...组件返回故障代码:0x80004005(NS_ERROR_FAILURE)" nsresult:0x80004005(NS_ERROR_FAILURE)"该错误也没有指出任何具体内容.通过从同一服务器调用进行了相同的测试并且工作正常.但我正在研究两个服务器架构.

任何解决方案/想法?看到很少有关于将跨域来源的响应标头设置为*"的帖子,但不确定如何为 xmlhhtprequest 调用设置响应标头?

解决方案

由于尝试通信的实体的起源在主机部分相同但在端口部分不同(localhost:8080 <-> localhost:9090) -- 这违反了同源策略,浏览器会抛出错误.有关更多信息,请参阅:http://en.wikipedia.org/wiki/Same_origin_policy>

解决这个问题的方法是在服务器返回的 HTTP 响应中使用 CORS 标头.您使用 XHR 生成 HTTP 请求这一事实没有区别.基本上,您需要做的是添加一个 Access-Control-Allow-Origin: *Access-Control-Allow-Origin: http://localhost:8080 到您对 http://localhost:9090 的 POST 请求的响应.这当然假设您可以控制要发布到的服务器资源,以便您可以修改响应标头.查看更多信息:http://enable-cors.org/http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

been stuck with an issue for sometime and not getting the best solution for this yet. I am calling an xmlhttprequest (POST)from a javascript residing at server say 'http://localhost:8080' to another server 'http://localhost:9090'

Getting an browser error As mentioned below: Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"    The error doesnt point anything specific either. Did test the same by calling from same server and worked fine. But i am looking at two server architecture.

Any resolution/ideas ? Saw few posts on setting response headers with cross domain origin as '*',but not sure exactly how to set response headers for xmlhhtprequest calls?

解决方案

Since the origins of the entities that are trying to communicate are the same in the host part but different in the port part (localhost:8080 <-> localhost:9090) -- this violates the same origin policy and the browser throws an error. See this for more info: http://en.wikipedia.org/wiki/Same_origin_policy

The way to get around this is to use CORS headers in the HTTP responses returned by the server. The fact that you are using XHR to generate the HTTP requests makes no difference. Basically, what you need to do is add a Access-Control-Allow-Origin: * or Access-Control-Allow-Origin: http://localhost:8080 to your responses for POST requests to http://localhost:9090. This of course assumes that you have control over the server resource you are POSTing to so that you can modify the response headers. See this for more info: http://enable-cors.org/ and http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

这篇关于xmlhttprequest 跨站域问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆