[英]CORS error when posting request in React app to PHP file in Apache server on localhost
I am creating simple app for fetching icos and firm names from one site.我正在创建一个简单的应用程序,用于从一个站点获取 icos 和公司名称。 I have this project already done with JQuery and now I am trying to learn React and do the same in it.
我已经用 JQuery 完成了这个项目,现在我正在尝试学习 React 并在其中做同样的事情。
First part of this project is form with ico and name inputs.该项目的第一部分是带有 ico 和名称输入的表单。 On submit, it create post request to php file, which checks, if inserted values are in database or not and if so, then checks if they are younger than one month.
提交时,它会向 php 文件创建发布请求,该文件检查插入的值是否在数据库中,如果是,则检查它们是否小于 1 个月。 After that, it returns rows from database or from website (from website it is up to date).
之后,它从数据库或网站(从网站是最新的)返回行。
But I have small problem.但我有一个小问题。 I cant create post request with React because this error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.0.52:80/backend/backend.php. (Reason: header 'access-control-allow-origin' is not allowed according to header 'Access-Control-Allow-Headers' from CORS preflight response).
我无法使用 React 创建发布请求,因为此错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.0.52:80/backend/backend.php. (Reason: header 'access-control-allow-origin' is not allowed according to header 'Access-Control-Allow-Headers' from CORS preflight response).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.0.52:80/backend/backend.php. (Reason: header 'access-control-allow-origin' is not allowed according to header 'Access-Control-Allow-Headers' from CORS preflight response).
I have WampServer running on port 80 where is my backend.php file and another direstory with React app.我在端口 80 上运行 WampServer,这是我的后端。php 文件和 React 应用程序的另一个可怕之处。 In WampServer is headers_module, which is enabled and in php file is header
header('Access-Control-Allow-Origin: *');
在 WampServer 中是 headers_module,它已启用,在 php 文件中是 header
header('Access-Control-Allow-Origin: *');
. . In axios post request I am including
"Access-Control-Allow-Origin": "*"
too.在 axios 发布请求中,我也包括
"Access-Control-Allow-Origin": "*"
。 Please does someone know, how to fix this?请问有人知道,如何解决这个问题? I cant figure it out.
我想不通。
Here is my axios code snippet:这是我的 axios 代码片段:
const options = {
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
}
};
axios.post("http://192.168.0.52:80/backend/backend.php", {
ico: this.state.ico,
name: this.state.name,
searchFirm: true
}, options)
.then((response) => {
}, (error) => {
});
And here is snippet from php file:这是 php 文件的片段:
<?php
header('Access-Control-Allow-Origin: *');
require_once "DbConnect.php";
echo "ahoj";
...
http://192.168.0.52:80/backend/backend.php in browser works fine.浏览器中的http://192.168.0.52:80/backend/backend.php工作正常。
EDIT: But what is interesting is that, when I send the request, it returns two lines in net tab and when I open the second line and there at the top right corner click "send again", it passes.编辑:但有趣的是,当我发送请求时,它在 net 选项卡中返回两行,当我打开第二行并在右上角单击“再次发送”时,它通过了。
After days of searching, I came up with a solution.经过几天的搜索,我想出了一个解决方案。 Hopefully it will help someone.
希望它会帮助某人。
All you need to do is add this two lines to php file:您需要做的就是将这两行添加到 php 文件中:
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: Content-Type');
And in axios request you dont need to use headers, so it can looks like this:在 axios 请求中,您不需要使用标头,因此它看起来像这样:
axios.post("your_url", {
your_data
})
.then((response) => {
}, (error) => {
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.