如何在Laravel 5和AngularJS上执行CORS POST请求?

时间:2022-08-22 17:46:15

i am use

我正在使用

github.com/barryvdh/laravel-cors

with default configuration

使用默认配置

and i was success with GET request,

我在GET请求上取得了成功,

when i do POST request,

当我做POST请求时,

on console network tab, that POST status is 200,

在控制台网络选项卡上,POST状态为200,

BUT i got an error

但是我收到了一个错误

XMLHttpRequest cannot load http://localhost:8000/api/data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

XMLHttpRequest无法加载http:// localhost:8000 / api / data。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许Origin'http:// localhost'访问。

2 个解决方案

#1


1  

You'll have to define the Access-Control-Allow-Origin header on your laravel application.

您必须在laravel应用程序中定义Access-Control-Allow-Origin标头。

To allow ANY domain to request resources from your laravel application:

要允许任何域从您的laravel应用程序请求资源:

$response = Response::make($contents, $statusCode);
$response->header('Access-Control-Allow-Origin', '*');
return $response;

#2


0  

You can just put the: Header set Access-Control-Allow-Origin "*"
setting in the Apache configuration or htaccess file.
Then check if your changes are correct: apachectl -t
and restart your webserver sudo service apache2 reload

您可以在Apache配置或htaccess文件中设置:Header set Access-Control-Allow-Origin“*”设置。然后检查您的更改是否正确:apachectl -t并重新启动您的webserver sudo服务apache2 reload

#1


1  

You'll have to define the Access-Control-Allow-Origin header on your laravel application.

您必须在laravel应用程序中定义Access-Control-Allow-Origin标头。

To allow ANY domain to request resources from your laravel application:

要允许任何域从您的laravel应用程序请求资源:

$response = Response::make($contents, $statusCode);
$response->header('Access-Control-Allow-Origin', '*');
return $response;

#2


0  

You can just put the: Header set Access-Control-Allow-Origin "*"
setting in the Apache configuration or htaccess file.
Then check if your changes are correct: apachectl -t
and restart your webserver sudo service apache2 reload

您可以在Apache配置或htaccess文件中设置:Header set Access-Control-Allow-Origin“*”设置。然后检查您的更改是否正确:apachectl -t并重新启动您的webserver sudo服务apache2 reload