reactjs - axios post call

时间:2017-10-24 09:24:41

标签: reactjs axios

我正在尝试做一个axios post post并得到如下所示的错误。我已经尝试过博客中可用的所有路由,但仍然遇到同样的错误。请建议。

var headers = {
    'Content-Type': 'application/json'
}
const dt = JSON.stringify({"name": "ddd"});
return axios.post('https://exam.com/apps', dt, headers)
    .then(function(response) {
        console.log("xxx", response.data["name"])
    })
    .catch(function(error) {
        console.log("axios error:",error)
    });

错误消息:

xhr.js:178 POST https://exam.com/apps 400 (Bad Request)
axios error: Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)

1 个答案:

答案 0 :(得分:0)

 var authOptions = {
   method: 'post',
   url: 'https://exam.com/apps',
   data: JSON.stringify({"name": "ddd"});,
   headers: {
    'Content-Type': 'application/json'
   },
   json: true
  };
  axios(authOptions)
     .then((response) => {
         console.log(response);
          })
     .catch((error) => {
          alert(error)
        })