在本地服务器中遇到CORS错误

在本地服务器中遇到CORS错误

本文介绍了在本地服务器中遇到CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将带有 XMLHttpRequest 的发帖请求发送到本地的其他域,但出现以下CORS错误:

I'm sending a post request with XMLHttpRequest to my other domain in local but I get the CORS error below:

我已经在PHP中设置了标头,如下所示:

I had set header in PHP as below:

<?php
header("Access-Control-Allow-Origin: *",false); //header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: POST, GET');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Max-Age: 1000');
header("Content-type:application/json");
readfile('data.json');

尽管设置了标题,我仍然收到CORS错误;我在做什么错,我该如何解决?

Despite setting the header, I'm still getting the CORS error; what am I doing wrong and how can I fix it?

推荐答案

最后,我解决了这个问题.
我使用Laragon进行PHP开发,但我不知道其Laragon配置或apache默认情况下会在配置文件中对此进行设置:

Finally, I fix this problem.
I use Laragon for PHP development and I don't know its Laragon configuration or apache that by default they set this in the config file:

Header always set Access-Control-Allow-Origin "*"

,这与我在PHP中设置的标头冲突.我对此进行了评论,并且可以毫无问题地设置标题.

and this makes conflicts with headers that I set in PHP. I comment it and can set headers with no problems.

感谢@Thamaraiselvam&@ rahul-mukherjee帮助了我.

thanks to @Thamaraiselvam & @rahul-mukherjee for helping me.

这篇关于在本地服务器中遇到CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:28