2017-01-05 163 views
2

我有這片Vue.js的:CORS失敗的POST請求

... 
this.$http.post('http://localhost:8000/oauth/token', postData) 
    .then(response => { 
    console.log(response) 
}) 
this.$http.get('http://localhost:8000/api/test') 
    .then(response => { 
    console.log(response) 
}) 
... 

最後一個GET請求工作正常,但文章沒有通過,由於去

的XMLHttpRequest無法加載http://localhost:8000/oauth/token。對預檢請求的響應不會通過訪問控制檢查:請求的資源上不存在「Access-Control-Allow-Origin」標頭。產地......

我Cors.php中間件:

... 
public function handle($request, Closure $next) 
{ 
    return $next($request) 
     ->header('Access-Control-Allow-Origin', '*') 
     ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 
} 
... 

Kernel.php:

... 

    'api' => [ 
     'throttle:60,1', 
     'bindings', 
     \App\Http\Middleware\Cors::class, 
    ], 
... 

我已經做了php artisan route:list我注意到,該航線...oauth/token具有中間件throttle,它是唯一一個默認的...我關注https://laravel.com/docs/5.3/passport#installation

Both(cli耳鼻喉科/服務器)的本地主機上的

+0

嘗試註冊中間件到Web中間件組,而不是一個API – Hammerbot

+0

@El_Matella,那麼這兩個請求失敗。我做了一個'php工匠路線:列表'和那條路線只有中間件是'throttle' – Miguel

+0

如果你在全球註冊它? – Hammerbot

回答

0

運行假設你正在使用VUE資源嘗試從devtools調試在網絡選項卡電話。

如果沒有CORS標頭存在於請求頭選項卡,就把這行某處Vue.use(VueResource)後:Vue.http.options.crossOrigin = true