VUE资源:访问控制允许来源错误

问题描述:

VUE资源:VUE资源:访问控制允许来源错误

Vue.http.post(API_URL + '/jwt/access_token', credentials, { 
      headers: { 
       'Access-Control-Allow-Origin': true 
      } 
     }).then(response => { 
      console.log(response.data) 
     }, err => reject(err)) 

我的API正确地与CORS laravel配置..

我得到这个错误:

XMLHttpRequest cannot load http://finance.app/jwt/access_token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. 

请求头:

OPTIONS /jwt/access_token HTTP/1.1 
Host: finance.app 
Connection: keep-alive 
Access-Control-Request-Method: POST 
Origin: http://localhost:8080 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 
Access-Control-Request-Headers: access-control-allow-origin, content-type 
Accept: */* 
Referer: http://localhost:8080/ 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 

我要去哪里? :(

感谢

+1

那么,怎么样的响应头? – gurghet

+0

问题解决了。 在vue-resource中没有设置,但laravel中有一个小的middlware配置错误。 谢谢。 – Giovanne

我想你应该在服务器端是这样一组信头(如果你使用PHP):

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); 
header('Access-Control-Allow-Headers: X-HTTP-Method-Override, Content-Type, x-requested-with, Authorization'); 

关键是第2行,手段可以访问POST/GET/OPTIONS请求

PS英语不是我的母语希望这将有助于

为我工作的解决方案是这些头添加到PHP:

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'); 
header('Access-Control-Allow-Methods: GET, POST, PUT'); 

而以下选项Vue公司,到后期数据传递给PHP:

Vue.http.options.emulateJSON = true