Axios帖子待定

时间:2018-08-24 11:41:59

标签: javascript vue.js axios

这是在Vue中使用Axios的简单Post请求:

import axios from 'axios'
export default {
    name: 'HelloWorld',
    props: {
        msg: String
    },
    mounted () {
        const code = 'test'
        const url = 'http://localhost:3456/'
        axios.post(url, code, { headers: {'Content-type': 'application/x-www-form-urlencoded', } }).then(this.successHandler).catch(this.errorHandler)
    },
    methods: {
        successHandler (res) {
            console.log(res.data)
        },
        errorHandler (error) {
            console.log(error)
        }
    }
}

Get方法可以正常工作。但是过帐在“网络”选项卡上保持为“待处理”状态。我可以确认我的Web服务上有一个Post方法,它返回了一些东西(在Postman上进行了测试)。

更新

将代码发送为参数:

axios(url, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
    },
    params: {
        code : 'test'
    },
}).then(this.successHandler).catch(this.errorHandler)

WEBSERVICE

server.post('/', (req, res, next) => {
    const { code }  = req.params

    const options = {
        validate: 'soft',
        cheerio: {},
        juice: {},
        beautify: {},
        elements: []
    }

    heml(code, options).then(
        ({ html, metadata, errors }) => {
            res.send({metadata, html, errors})
            next()      
        })
})

2 个答案:

答案 0 :(得分:1)

我认为您的axios请求结构有问题。 试试这个:

const URL = *YOUR_URL*;
axios(URL, {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    data: *YOUR_PAYLOAD*,
  })
    .then(response => response.data)
    .catch(error => {
      throw error;
    });

如果您要发送查询参数:

axios(URL, {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    params: {
     code: 'your_string'
    },
  })

如果它是路径变量,则可以设置网址:

const url = `http://localhost:3456/${code}`

让我知道问题是否仍然存在

答案 1 :(得分:0)

我也遇到了同样的情况。网络调用一直处于待处理状态,并通过将响应从 server.js(路由文件)传回来缓解它,例如(res.json(1);)并解决了问题