我有一个Laravel 5.4 API,可以在Postman和Browser中正常工作。 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)

来源 header
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

端点是Larvel API服务器,可以正常工作。

任何线索都将有所帮助,因为我不知道如何解决此问题。提前致谢。

最佳答案

服务器正在从以下位置发送重定向:

http://backend-dev.xolas.io/api/v1/view/calendar/-30/90/

至:
http://backend-dev.xolas.io/api/v1/view/calendar/-30/90

在请求中删除结尾的“/”应该可以防止出现301。(尽管该网址以500服务器错误响应。

关于vue.js - 使用vue.js的axios预检失败错误301,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45013871/

10-12 00:13
查看更多