使用内容类型为:application / x-www-form-urlencoded

时间:2019-01-14 14:39:43

标签: javascript react-native fetch

我正在调用带有 POST 请求的简单登录 API ,以下是这些参数: 标题: 内容类型:application / x-www-form-urlencoded 身体: 电子邮件:字符串 密码 从服务器返回的错误是:422无法处理的实体

代码:

  var formBody = new FormData();
formBody.set("email", "test5@gmail.com");
formBody.set("password", "12345678");


const data = new URLSearchParams(new FormData(details));
return  dispatch => {
    dispatch(requestData());
    try {
        fetch(`${BASE_URL}users/sign_in`, {
            method: 'POST',
            // headers: Interceptor.getHeaders(),
            headers: {
                Accept:'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
              },
            //   body: formBody
             body: data

        })
            .then(res => res.json())
                .then(result=>
                {
                    if (result.success === false) {}
                }
                )


    } catch (error) {
        console.log('error',error)
        dispatch(failureData(error))
    }
}

Screenshot of code

1 个答案:

答案 0 :(得分:0)

得到答案,422基本上是由语义问题引起的,在我的情况下,我的请求标头的来源为空。