React Native - 获取 POST 请求作为 GET 请求发送 [英] React Native - Fetch POST request is sending as GET request

查看:49
本文介绍了React Native - 获取 POST 请求作为 GET 请求发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 FETCH 时遇到问题.

I'm having issues when using FETCH.

我正在尝试在 react-native 中使用 FETCH 发出 POST 请求.

I am trying to make a POST request using FETCH in react-native.

    fetch("http://www.example.co.uk/login", {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            username: 'test',
            password: 'test123',
        })
    })

        .then((response) => response.json())
        .then((responseData) => {
            console.log(
                "POST Response",
                "Response Body -> " + JSON.stringify(responseData)
            )
        })
        .done();
}

当我使用 Charles 检查此调用时,它被记录为 GET 请求,而正文中应包含的用户名和密码不存在.

When I inspect this call using Charles it is recorded as a GET request and the username and password that should be in the body are not there.

有人可以帮忙解决这个问题吗?

Can anyone help with this issue?

推荐答案

这对我有用:

let data = {
  method: 'POST',
  credentials: 'same-origin',
  mode: 'same-origin',
  body: JSON.stringify({
    appoid: appo_id
  }),
  headers: {
    'Accept':       'application/json',
    'Content-Type': 'application/json',
    'X-CSRFToken':  cookie.load('csrftoken')
  }
}
return fetch('/appointments/get_appos', data)
        .then(response => response.json())  // promise
        .then(json => dispatch(receiveAppos(json)))
} 

这篇关于React Native - 获取 POST 请求作为 GET 请求发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆