【问题标题】:react native fetch not sending body content反应原生获取不发送正文内容
【发布时间】:2016-12-20 13:29:08
【问题描述】:

我是新来的 react native 并关注 this tutorial
我正在尝试将json正文发送到私有api服务器,我检查了服务器日志,发现正文内容为空。

这是 react native 中的代码

authenticateLogIn(){
    fetch('<URL>', {
          method: 'POST',
          header: {'Content-Type': 'application/json', 'Accept': 'application/json'},
          body: JSON.stringify({'username': '<username>', 'password':'<password>'})
        })
        .then((incoming) => incoming.json())
        .then((response) => {
          console.log(response.header);
          Alert.alert(JSON.stringify(response.body));
        })
        .done();

【问题讨论】:

    标签: react-native


    【解决方案1】:

    也许是因为它应该是标题s? (带 s)

    【讨论】:

    • 啊,就是这样,一直以为它只是一个没有S的标题
    • 你是对的,当 remove s 时它作为 post 方法传递,但仍然传递给后端的数据是空的
    【解决方案2】:

    这可能是因为您的 JSON 正文没有被正确解析。 bodyParser https://github.com/expressjs/body-parser 是解析请求体的中间件的一部分,需要配置。

    req.body 一直为我返回空,直到我将 app.use(bodyParser.json() 添加到 server.js 文件中。确保导入body-parser

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多