在两个顺序请求上出现axios问题

时间:2019-03-11 16:05:21

标签: php vue.js axios

当两个顺序请求发送而第二个请求获取错误时,我有问题->错误:“网络错误” 例如:

OPTION : /meal/get      200 success
GET: /meal/get    200 success
OPTION: /message/get  200 get
dont send GET :/message/get 

显示错误

我检查OPTIONS的标头响应:第一个选项的响应部分为true,但第二个选项为错误

我将php用于后端,而所有部分均能真正发挥作用

全局配置文件:

axios.defaults.headers.get['Accept'] = 'application/json'
axios.defaults.headers.common['Content-Type'] = 'application/json'

这是我的代码

           axios({
                method: 'get',
                url: '/message/index',
                data: {},
                params: {
                    page: page
                }
            }).then(res => {

响应标题:

Access-Control-Allow-Credentials    true
Access-Control-Allow-Headers    Origin, Accept, Content-Type, …, X-GR-   Token, Accept-Language
Access-Control-Allow-Methods    GET,POST,OPTIONS
Access-Control-Allow-Origin *
Access-Control-Expose-Headers   X-Access-Token, X-Refresh-Toke…nation-Total-Count, X-Payload
Connection  Keep-Alive 
Content-Length  0
Content-Type    text/html; charset=UTF-8
Date    Mon, 11 Mar 2019 16:00:22 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.4.27 (Win64) PHP/7.1.9
Status  200 OK
X-Powered-By    PHP/7.1.9

每个请求在不同的组件中

        axios({
            method: 'get',
            url: '/meal/suggest',
            data: {},
            params: {}
        }).then( res => {
            console.log(res)
            // this.rightSideData = res.data
        }).catch(err => {
            console.log(err)
            console.log(err.response.data)
        })

此请求的标题响应:

Connection  Keep-Alive
Content-Length  876
Content-Type    text/html; charset=UTF-8
Date    Mon, 11 Mar 2019 16:00:23 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.4.27 (Win64) PHP/7.1.9
X-Powered-By    PHP/7.1.9

1 个答案:

答案 0 :(得分:0)

我找到了解决方案 我在php phalcon中设置了标头,它是对第一个请求的响应,但是如果我同时发送两个请求,则它只是回答其中一个,而第二个失败

我进行了大量搜索并了解到我也将标头放在.htaccess中 这是我的代码:

#Header set Content-Security-Policy "default-src 'self';"
Header always set Access-Control-Allow-Origin "http://localhost:8080"
Header always set Access-Control-Allow-Headers "Content-Type, Accept-Language, X-Access-Token, X-Client-Id, X-Secret-Id, X-GR-Token"
Header always set Access-Control-Allow-Methods "GET,POST,OPTIONS"
Header always set Access-Control-Expose-Headers "X-Access-Token, X-Refresh-Token,X-Access-Token-Expire, X-Pagination-Current-Page, X-Pagination-Page-Count,X-Pagination-Per-Page, X-Pagination-Total-Count, X-Payload"
Header always set Access-Control-Allow-Credentials "true"