vue-resource:Access-Control-Allow-Origin错误

时间:2016-08-02 11:57:36

标签: laravel cors laravel-5.2 vue.js vue-resource

VUE资源:

baseURI

我的api已经正确配置了CORS laravel ..

我收到了这个错误:

href

请求标题:

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

谢谢!

2 个答案:

答案 0 :(得分: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来请求。

P.S。英语不是我的母语希望它会有所帮助

答案 1 :(得分: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