Origin is not allowed by Access-Control-Allow-Origin(Access-Control-Allow-Origin 不允许来源)
问题描述
我正在向 中的远程 PHP 服务器发出 Ajax.request
Sencha Touch 2 应用程序(包装在 PhoneGap 中).
I'm making an Ajax.request
to a remote PHP server in a Sencha Touch 2 application (wrapped in PhoneGap).
服务器的响应如下:
XMLHttpRequest 无法加载 http://nqatalog.negroesquisso.pt/login.php.Access-Control-Allow-Origin 不允许来源 http://localhost:8888
.
XMLHttpRequest cannot load http://nqatalog.negroesquisso.pt/login.php. Origin
http://localhost:8888
is not allowed by Access-Control-Allow-Origin.
我该如何解决这个问题?
How can I fix this problem?
推荐答案
前段时间我写了一篇关于这个问题的文章,跨域 AJAX.
I wrote an article on this issue a while back, Cross Domain AJAX.
如果您可以控制响应服务器,则处理此问题的最简单方法是为以下内容添加响应标头:
The easiest way to handle this if you have control of the responding server is to add a response header for:
Access-Control-Allow-Origin: *
这将允许跨域 Ajax.在 PHP 中,您需要像这样修改响应:
This will allow cross-domain Ajax. In PHP, you'll want to modify the response like so:
<?php header('Access-Control-Allow-Origin: *'); ?>
您只需将 Header set Access-Control-Allow-Origin *
设置放在 Apache 配置或 htaccess 文件.
You can just put the Header set Access-Control-Allow-Origin *
setting in the Apache configuration or htaccess file.
应该注意的是,这会有效地禁用 CORS 保护,这很可能会使您的用户受到攻击.如果您不知道自己特别需要使用通配符,则不应使用它,而应将您的特定域列入白名单:
It should be noted that this effectively disables CORS protection, which very likely exposes your users to attack. If you don't know that you specifically need to use a wildcard, you should not use it, and instead you should whitelist your specific domain:
<?php header('Access-Control-Allow-Origin: http://example.com') ?>
这篇关于Access-Control-Allow-Origin 不允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Access-Control-Allow-Origin 不允许来源


- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01