javascript - 使用 fetch api 在 react 中发帖

标签 javascript node.js reactjs fetch

我有一个消息方法,当我单击表单上的提交按钮时会触发该方法。但是我无法访问我的 Node rest api 中的 url,我试图通过返回未定义的“req.body.url”访问 url,当我看到 req.body 中没有任何内容时。我已经使用 axios 完成了它,它工作正常帮助我使用 fetch

    message(event){
       event.preventDefault();
           const uri = this.state.short.url;
             fetch("http://localhost:3000/messages", {
              method: "post",
              body: {url: uri}
              })
            .then(function(data) {
          console.log("Request succeeded with  response", data);
           })
       .catch(function(error) {
           console.log("Request failed", error);
         });
    }

最佳答案

在您的获取中,您需要指定 Json 并像这样对您的 json 进行字符串处理:

body: JSON.stringify({ url: uri }),
 headers:{
   'Content-Type': 'application/json'
}

关于javascript - 使用 fetch api 在 react 中发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51997775/

相关文章:

javascript - 导航折叠不起作用 [Twitter Bootstrap]

KO 模板中的 JavaScript

javascript - 如何使用 jest 全局模拟单个实用函数

javascript - ES6 类内部工作原理 : prototype assignments understanding

javascript - 使用 JavaScript 将 HTML 字符串加载到 iframe

javascript - 快速服务器并发

node.js - require 的 'import * as ...' 等价物是什么?

node.js - 如何 promise Braintree 方法?

node.js - 无法使用 Node :alpine 在 Docker 容器中运行 React 应用程序的开发版本

javascript - React 16 Hidden button Modal 不会在点击时打开