axios 发送post请求

在axios发送post的请求时,他的消息头默认值是"application/json;charset=UTF-8" ,当你后台需要表单形式的提交的时候,就会报错400的错误,所以需要改消息头,而$ajax的默认消息头是application/x-www-form-urlencoded

axios({
     method:'post',
     url:'http://localhost:8080/login/login',
     data:{
         account:'17050285',
         password:'123456',
     },transformRequest: [function (data) {//转换formdata的格式
        var ret = ''
        for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
    }],
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
 }).then(function (response) {
     console.log(response);
 })

猜你喜欢

转载自my.oschina.net/u/3792723/blog/1649743