【发布时间】:2018-08-26 14:55:48
【问题描述】:
这是反应获取:
变种 JSON = { json: JSON.stringify({ 一个:1, 乙:2 }), 延迟:3 }; fetch('/saveInfo', { 方法:'发布', 标题:{ '接受':'应用程序/json,文本/纯文本,*/*', “内容类型”:“应用程序/json” }, 正文:JSON.stringify(json.json) }) .then(函数(响应){ 返回响应.json(); }) .then(函数(结果){ 警报(结果); console.log("文件已保存!"); }) .catch(函数(错误){ console.log('请求失败'); });这是节点:
<pre>
var express = require('express');
module.exports = function(app) {
var router = express.Router();
router.get('/', function (req, res) {
console.log('from node');
console.log(req);
res.json({status: 'UP'});
});
app.use("/saveInfo", router);
}
</pre>
上面的代码不适用于 fetch 的第二个参数。
但是当我执行它时不带第二个参数来获取如下:
fetch('/saveInfo')
.then(function (response) {
return response.json();
})
.then(function (result) {
alert(result);
console.log("The file was saved!");
})
.catch (function (error) {
console.log('Request failed');
});
工作正常,能够与节点程序通信。
谁能帮我解决这个问题。我想将 react 的 UI 表单状态发送给节点程序。
【问题讨论】: