Chrome 报错提示
Access to XMLHttpRequest at 'http://api.test/?s=App.User_User.Login' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
mock.js?411c:8404 POST http://api.test/?s=App.User_User.Login net::ERR_FAILED
VueJs 代码
import axios from 'axios'
axios
.post(defaultSettings.phpApi + 'App.User_User.Login', {
username: body.username,
password: '1234567890'
})
NGINX配置
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
# !!!!重要 普通Ajax请求直接发起请求不用添这个设置。
# Vuejs会先发起option请求,而add_header支持的http状态码有限,必须添加http_code返回204
if ($request_method = 'OPTIONS') {
return 204;
}
VueJs option 请求样本