2016-08-02 112 views
0

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

回答

2

我想你應該在服務器端是這樣一組信頭(如果你使用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英語不是我的母語希望這將有助於

0

爲我工作的解決方案是這些頭添加到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 
相關問題