【问题标题】:Always failed when POST data with json (body: raw)使用 json (body: raw) POST 数据时总是失败
【发布时间】:2018-01-13 11:09:30
【问题描述】:

向我的服务器发送数据时遇到问题。我正在使用 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

如何解决这个问题?谢谢:)

【问题讨论】:

  • 任何服务器日志?
  • 你试过JSON.stringify(input)吗?
  • 很确定上面的评论是解决方案。如果你运行 Telerik Fiddler,你可以看到实际的请求,当它不是你期望的时候,这可能会帮助你调试,并让你快速看到邮递员请求和你自己的请求之间的区别。
  • url 好像有错别字。
  • 我已经是 JSON.stringify(input) 了,但结果还是一样的。我认为 headers 有一些配置。

标签: javascript api react-native fetch axios


【解决方案1】:

@大卫

没错,我也发现了一个 Axios 的 bug。。

我发现的错误是 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" }
})

【讨论】:

    【解决方案2】:

    我发现axios在处理json for body时有些问题 试试这个:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多