php Access-Control-Allow-Origin 不允许 Chrome Origin null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18945158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome Origin null is not allowed by Access-Control-Allow-Origin
提问by Canvas
I have ready many of the other posts ppl have asked here about the origin not allowed blablabla,
我已经准备好了很多其他人在这里问过的帖子,关于不允许 blablabla 的起源,
Now I have tried to add --Access-Control-Allow-Origin and enable apps and even disabled security but every time I try my button on the php page it just keeps stating
现在我尝试添加 --Access-Control-Allow-Origin 并启用应用程序,甚至禁用安全性,但每次我在 php 页面上尝试我的按钮时,它一直在说明
Origin null is not allowed by Access-Control-Allow-Origin.
Can anyone help me at all? Here is the code that is causing the problem
任何人都可以帮助我吗?这是导致问题的代码
page.php
页面.php
<html land="en">
<head>
<meta carset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css";
</head>
<body>
<!-- Document Ready Event -->
<input id="text" type="text" /><input id="submit" type="button" value="Submit" />
<div id="feedback"></div>
<script src="../jquery-1.10.2.min.js"></script>
<script src="script.js"></script>
</body>
</html>
Script.js
脚本.js
$('#submit').click( function()
{
var text = $('#text').val();
$.get( 'PHP/reverse.php', { input: text }, function( data )
{
$('#feedback').text( data );
});
});
回答by Quentin
Origin null is not allowed by Access-Control-Allow-Originmeans that you are trying to perform Ajax on a local file. This is forbidden for security reasons. Even if this was not the case, your PHP wouldn't run because PHP is supported by web servers, not web browsers.
Origin null is not allowed by Access-Control-Allow-Origin意味着您正在尝试对本地文件执行 Ajax。出于安全原因,这是禁止的。即使情况并非如此,您的 PHP 也不会运行,因为 Web 服务器支持 PHP,而不是 Web 浏览器。
You have a web server installed. You have to request your pages through the server, rather than accessing them directly from your file system.
您已安装 Web 服务器。您必须通过服务器请求您的页面,而不是直接从您的文件系统访问它们。
Use a URL starting with http://localhost/
使用以开头的 URL http://localhost/
You will need to move your files so that they are under the server's DocumentRoot(or reconfigure the server so that it can access them from their present location).
您将需要移动您的文件,使它们位于服务器的下方DocumentRoot(或重新配置服务器,以便它可以从当前位置访问它们)。
回答by Aaron
I'm not sure which browser you are testing for. In my case, it works for all my browsers if I sent AJAX calls from local file, which means the Origin is null. I guess the reason you can not get it work is that some server side coding are needed.
我不确定您正在测试哪种浏览器。就我而言,如果我从本地文件发送 AJAX 调用,它适用于我的所有浏览器,这意味着Origin is null. 我想你不能让它工作的原因是需要一些服务器端编码。
Try this, add the Access-Control-Allow-Originheader to the HTTP response and set the value to *. I'm not sure how to do this in PHP, but here's a piece of Java code for your information.
试试这个,将Access-Control-Allow-Origin标头添加到 HTTP 响应并将值设置为*. 我不确定如何在 PHP 中执行此操作,但这里有一段 Java 代码供您参考。
response.setHeader("Access-Control-Allow-Origin", "*")
response.setHeader("Access-Control-Allow-Origin", "*")
回答by shamaleyte
There is this chrome plugin allows to request any site with ajax from any source. Adds to response 'Allow-Control-Allow-Origin: *' header
有这个 chrome 插件允许从任何来源请求任何带有 ajax 的站点。添加到响应 'Allow-Control-Allow-Origin: *' 标头
It worked for me without needing to change anything on the server side. But you should change withCredentials: false in the request, if it conforms with your tests.
它对我有用,无需在服务器端更改任何内容。但是您应该在请求中更改 withCredentials: false ,如果它符合您的测试。

