【发布时间】:2017-12-14 07:05:50
【问题描述】:
我有一个 Laravel 5.4 API,它在 Postman 和浏览器中运行良好。 Localhost 运行良好——Laravel 5.4 在一个端口上运行,而 Vue 在热部署模式下运行良好。
但是,当我将 Vue 代码移动到我的生产服务器时,我收到了这个错误:
Response for preflight is invalid (redirect)
在 Chrome 开发者工具中,网络标签显示如下:
常规
Request URL:http://backend-dev.xolas.io/api/v1/view/calendar/-30/90/
Request Method:OPTIONS
Status Code:301 Moved Permanently
Remote Address:217.182.66.247:80
Referrer Policy:no-referrer-when-downgrade
响应标头
Connection:Keep-Alive
Content-Length:349
Content-Type:text/html; charset=iso-8859-1
Date:Mon, 10 Jul 2017 13:40:08 GMT
Keep-Alive:timeout=5, max=100
Location:http://backend-dev.xolas.io/api/v1/view/calendar/-30/90
Server:Apache/2.4.25 (Ubuntu)
原始标头
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:access-control-allow-origin,authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:backend-dev.xolas.io
Origin:http://localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Mobile Safari/537.36
我在 Laravel 上安装了一个 CORS 插件,还是不爽。
我的axios配置如下:
axios.defaults.headers.common['Authorization'] = store.apiKey
axios.defaults.headers.get['Content-Type'] = 'application/json;charset=utf-8'
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'
axios.defaults.headers.common['Access-Control-Allow-Origin'] = 'https://' + store.endpoint + ', http://' + store.endpoint
Endpoint 是 Larvel API 服务器,工作正常。
任何线索都会有所帮助,因为我不知道如何解决这个问题。提前致谢。
【问题讨论】:
标签: vue.js cors laravel-5.4 axios preflight