我尝试将laravel-cors(https://github.com/barryvdh/laravel-cors(与Laravel 5.2一起使用来更新数据,但我总是得到同样的错误:
XMLHttpRequest 无法加载 http://dev.pesanlab.com/api/v1/order/cart/add。请求的资源上不存在"访问控制允许源"标头。因此,不允许访问源"http://localhost"。响应具有 HTTP 状态代码 500。
GET 请求工作正常,但 POST 返回错误。
return [
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE'],
'exposedHeaders' => ['*'],
'maxAge' => 0,
'hosts' => [],
];
这是我在Route.php
上的代码
Route::group(['prefix' => 'api/v1'], function () {
Route::group(['middleware' => ['web', 'cors']], function () {
Route::post('order/cart/add','OrderController@cart_add');
});
});
你能帮我吗?
您的主要问题不是请求,而是给出的错误。错误:
XMLHttpRequest cannot load http://dev.pesanlab.com/api/v1/order/cart/add. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 500.
末尾显示状态代码 500(内部服务器错误(。从调试开始,并尝试找到真正的错误:
在拉拉维尔中启用调试:
配置/应用程序.php:
'debug' => env('APP_DEBUG', true),
或者使用默认的 php 显示错误:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
或者检查拉拉维尔.log
Storage/logs/laravel.log
设法执行此操作后,您可以看到原始错误: