简体   繁体   English

将 React 应用程序中的请求发布到本地主机上 Apache 服务器中的 PHP 文件时出现 CORS 错误

[英]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 选项卡中返回两行,当我打开第二行并在右上角单击“再次发送”时,它通过了。

Screen from net tab来自网络选项卡的屏幕

Solution解决方案

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.

相关问题 从反应应用程序向快递服务器发送请求时如何修复CORS错误 - How to fix CORS error when sending a request to express server from react app 无法通过 CORS 请求错误与 Phonegap App 中的服务器连接 - Can't connect with server in Phonegap App by CORS request error 如何修复 localhost 中的反应 cors 错误? - How to Fix react cors error in localhost? 当客户端和服务器都为本地主机时会发生CORS? - CORS when both client & server are localhost? 尝试从 localhost:8080 向 localhost:3000 发出 GET 请求,但我收到 cors 错误,并在我的 node.js 服务器上安装 cors 无法修复它 - Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it 前端在 CORS 请求中从后端获取错误 401 - apache/php 配置不足? - Frontend gets Error 401 from Backend in CORS request - apache/php insufficiently configured? 当我想将 localhost:4000 重定向到 localhost:3000 时出现错误 CORS - Error CORS when i want redirecting localhost:4000 to localhost:3000 如何将响应应用程序部署到localhost中的apache服务器 - How to deploy react apps into apache server in localhost 通过XMLHttpRequest从文件:// URL发送GET到本地主机时出现CORS错误 - CORS error when sending GET to localhost from file:// URL via XMLHttpRequest 从我的本地主机客户端发出请求而没有 cors 错误 - Make a request from my localhost client without cors error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM