bob*_*ech 9 cors laravel vue.js vue-resource
我不懂.我几个小时以来一直在努力
我在Laravel中使用Vue.js并尝试向外部API发出POST请求.
但我总是在我的Vue POST请求上收到CORS错误
methods: {
chargeCustomer(){
this.$http.post('/api/chargeCustomer', this.payment).then(function (response) {
console.log(response.data)
},function (response) {
console.log(response.data)
});
}
}
Run Code Online (Sandbox Code Playgroud)
错误
MLHttpRequest无法加载 https://www.mollie.com/payscreen/select-method/JucpqJQses.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许访问" https://payment.dev ".
我为我的后端安装了Laravel CORS包,并将中间件添加到我的路线中,例如
Route::group(['middleware' => 'cors'], function(){
Route::post('/api/chargeCustomer', 'Backend\PaymentController@chargeCustomer');
});
Run Code Online (Sandbox Code Playgroud)
但我仍然得到错误.我还尝试添加Vue Headers
Vue.http.headers.common['Access-Control-Allow-Origin'] = '*';
Vue.http.headers.common['Access-Control-Request-Method'] = '*';
Run Code Online (Sandbox Code Playgroud)
具有相同的结果/错误.
有人能告诉我我做错了什么吗?
您需要从中间件设置 CORS 标头。也许你需要一些额外的设置?
无论如何,您可以创建自己的中间件并在handle()方法中设置 CORS 标头,如下例所示:
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', 'http://yourfrontenddomain.com') // maybe put this into the .env file so you can change the URL in production.
->header('Access-Control-Allow-Methods', '*') // or specify `'GET, POST, PUT, DELETE'` etc as the second parameter if you want to restrict the methods that are allowed.
->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization') // or add your headers.
}
Run Code Online (Sandbox Code Playgroud)
将您的自定义中间件添加到Kernel.php 类中的全局$middleware数组(在 下CheckForMaintenanceMode::class),您应该很高兴。
| 归档时间: |
|
| 查看次数: |
4646 次 |
| 最近记录: |