首页 > 解决方案 > 使用 post 请求响应 native-fetch api 调用失败

问题描述

我创建了一个 fecth api 调用,如下所示

var model=new UserAnswer();
  model.category_id=2;
  model.exam_id=this.state.datasourece.exam.id;
  model.questions=this.state.answers;
  fetch(
    API.BASE_URL +
    API.SUBMIT_EXAM,
    {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Authorization: 'Bearer ' + this.props.Login.User.access_token,
      },
      body:JSON.stringify(model)
    },
  )

...等等..但它不能完全按预期工作,但我在邮递员中测试过同样的结果,我得到了结果。

JSON.stringify(model) 看起来像

{
"category_id": 2,
"exam_id": 9,
"questions": [
    {
        "question_id": 38,
        "user_answer_option_id": 145
    },
    {
        "question_id": 308,
        "user_answer_option_id": 1142
    },
    {
        "question_id": 309,
        "user_answer_option_id": 1146
    }
]

}

标签: react-nativefetch

解决方案


我已经弄清楚是什么问题。我刚刚更改了标题接受,如下所示

{
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + this.props.Login.User.access_token,
  },
  body:JSON.stringify(model)
}

一切都按预期工作


推荐阅读