我正在使用React JS + Laravel后端开发一个Web应用程序。现在,我正在尝试向Laravel后端API发送API Post请求。我正在使用axios在React KS中发出API请求。但这给了我CORS错误。
我正在React js中发出这样的发布请求
Axios.create({ headers: {'api-version': '2' }})
.post("url", requestBody)
.then((response)=> {
alert('Log in successful.')
})
.catch((error) => {
alert('Something went wrong')
});
在Laravel后端中,我使用此库https://github.com/barryvdh/laravel-cors配置了CORS。
这是cors.php配置文件。
<?php
return [
/*
|--------------------------------------------------------------------------
| Laravel CORS
|--------------------------------------------------------------------------
|
| allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
| to accept any value.
|
*/
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedOriginsPatterns' => [],
'allowedHeaders' => ['*'],
'allowedMethods' => ['*'],
'exposedHeaders' => [],
'maxAge' => 0,
];
当我使用axios发出post API请求时,它仍然给我这个错误。
Failed to load http://localhost/momento/memento-laravel/public/api/account/post: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
这是什么问题?我也正确配置了CORS。我可以提出GET请求,但是。我的代码有什么问题,我该如何解决?
这是我在浏览器中获得的响应标头