2017-09-05 116 views
2

我们正在使用的一个项目,该项目已经使用的WebPack的vue-bulma-admin支架,我们似乎无法执行任何POST/PUT请求我们的API(已设置访问交叉允许来源CORS问题头文件*),只有GET请求的作品。从消耗我们的API不同的前端,我们没有这个问题,只能用这种特定的前端,这让因为这常见的错误:使用VUE

"No 'Access-Control-Allow-Origin' header is present on the requested resource."

Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested 
resource. 

我们已经尝试设置自定义页眉在axios/vue-resource,但没有成功。正如我所说的,通过Postman /不同的前端或CORS chrome扩展使用相同的路线,一切正常。

任何想法,以解决此问题?

我中间件一个Cors在Laravel API。

<?php 

namespace App\Http\Middleware; 

use Closure; 

class Cors 
{ 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     return $next($request) 
      ->header('Access-Control-Allow-Origin', '*') 
      ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 
    } 
} 
+0

你还设置访问控制允许的方法解决我的问题? – Nora

+0

我用它编辑了我的问题。 –

+0

这听起来像你的帖子添加至少一个头(可能是Content-Type头?),这将触发你的浏览器,它自己自动,做一个CORS预检OPTIONS请求https://developer.mozilla.org/en -US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests该服务器的后端代码未配置为发送OPTIONS请求的Access-Control-Allow-Origin标头。必须将它配置为使用200 OK和Access-Control-Allow-Headers和Access-Control-Allow-Methods标头以正确的值响应OPTIONS请求。 – sideshowbarker

回答