用json(body:raw)POST数据时总是失败

时间:2017-08-05 18:04:44

标签: javascript api react-native fetch axios

将数据发送到服务器时遇到问题。我正在使用react native& axios ver ^ 0.16.2

let input = {
  'longitude': -6.3922782,
  'latitude': 106.8268856,
  'content': 'uget - uget sampai kaki lemes',
  'pictures': []
}

  axios({
    method: 'POST',
    url,
    headers: {
      'Content-Type': 'application/json',
      'Authorization': this.state.headers.authorization
    },
    data: input
  })                                                                     
  .then((resultAxios) => {
    console.log('hasil axios', resultAxios)
  })

并且状态结果始终为错误500。

如果我尝试用邮递员发送数据,一切都很好。在邮差中,我设置了

headers: {
  Authorization: ''',
  Content-Type: application/json
}

body = raw, JSON

postman setting

怎样才能解决这个问题?谢谢:))

2 个答案:

答案 0 :(得分:1)

我发现axios在处理身体的json时有一些问题 试试这个:

let input = 'longitude=-6.3922782&latitude=106.8268856&content="uget - uget sampai kaki lemes"&pictures=[]';

  axios({
    method: 'POST',
    url,
    headers: {
      'Content-Type': 'application/json',
      'Authorization': this.state.headers.authorization
    },
    data: input
  })                                                                     
  .then((resultAxios) => {
    console.log('hasil axios', resultAxios)
  })
and the status result always error 500.

If I try send data with postman, everything is fine. In postman, I set

headers: {
  Authorization: ''',
  Content-Type: application/json
}

body = raw, JSON

答案 1 :(得分:0)

@Davide

那是正确的,也是我发现的Axios错误。

我发现的错误是CORS飞行前错误,与实际问题无关

**失败**

Axios({
method: "post",
url: "https://localhost:44394/EXAMPLE/",
data: jsonPayload,
headers: { "Content-Type": "application/json" }
})

**成功**

Axios({
method: "post",
url: "https://localhost:44394/EXAMPLE/",
data: JSON.Stringify(jsonPayload),
headers: { "Content-Type": "application/json" }
})
相关问题