0

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

我哪里错了?:(

谢谢!

4

2 回答 2

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英语不是我的母语希望它会有所帮助

于 2016-08-04T14:13:18.207 回答
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
于 2017-02-27T08:55:11.450 回答