Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
expressjs http post无法从javascript post获取数据_Javascript_Node.js_Express_Fetch - Fatal编程技术网

expressjs http post无法从javascript post获取数据

expressjs http post无法从javascript post获取数据,javascript,node.js,express,fetch,Javascript,Node.js,Express,Fetch,试图从http post中记录节点脚本中的数据 const express = require("express"); const app = express(); const port = 3700; let io = require('socket.io').listen(app.listen(port)); const path = require('path'); const bodyParser = require('body-parser'); app.use(

试图从http post中记录节点脚本中的数据

const express = require("express");
const app = express();
const port = 3700;
let io = require('socket.io').listen(app.listen(port));

const path = require('path');
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static('public'));

app.post('/do-login', function(req, res) {
    console.log( req.body );
});
在客户端中,我使用javascript获取将数据发送到节点后端

let data = {
    email :'test@domain.com',
    password : 'test'
}

fetch('http://localhost:3700/do-login',{
    method : 'post',
    body : JSON.stringify(data)
}).then(function(response) {
    return response.json();
}).then(function(data) {
    console.log(data);
});
但是运气不好,它返回一个空对象

但是,如果我使用post
xmlHttpRequest

var http = new XMLHttpRequest();
var url = 'http://localhost:3700/do-login';
var params = 'email=test@domain.com&password=test';
http.open('POST', url, true);

//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);
我能够接收客户发送的数据。怎么了


非常感谢任何帮助和想法。

以url编码的形式发送请求。首先,创建一个处理
formUrlEncoding
的方法

formUrlEncoder(params, formUrlBody){
    
    for (let property in params) {
      let key = encodeURIComponent(property);
      let value = encodeURIComponent(params[property]);
      formUrlBody.push(key + "=" + value);
    }
    return formUrlBody.join("&");
}
接下来,调用将返回所有编码值的方法,因此只需将其传递到正文中

const url = 'http://localhost:3700/do-login';
const params = {
  email :'test@domain.com',
  password : 'test'
};
const headers = new Headers({
  'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
});

let formUrlBody = [];
formUrlBody = formUrlEncoder(params, formUrlBody);

fetch(url, {
  method: 'POST',
  headers: headers,
  body: formUrlBody
});

您可以找到有关获取的更多详细信息。

您的获取正在发送JSON。。。您的XHR正在发送
应用程序/x-www-form-urlencoded
。。。这两段代码不可比较解决方案1)使用fetch时发送x-www-form-urlencoded数据,或2)在fetch中将内容类型设置为application/jsonrequest@JaromandaX我愿意接受你的回答,请把它贴出来。其他人的回答也一样好-我不会回答。你能至少把它标记为副本,或者说你是从这里复制粘贴的吗