Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Javascript 如何通过POST将字符串从客户端发送到服务器_Javascript_Node.js_Express - Fatal编程技术网

Javascript 如何通过POST将字符串从客户端发送到服务器

Javascript 如何通过POST将字符串从客户端发送到服务器,javascript,node.js,express,Javascript,Node.js,Express,我想从NodeJS应用程序的前端向后端发送一个字符串 在服务器端,我的代码如下所示: app.post('/mydb/post', function(req, res, next) { console.log(req.body); }); 应通过以下方式发送字符串: var http = new XMLHttpRequest(); var url = '/mydb/post'; var params = 'john'; http.open('POST', url, true); //Se

我想从NodeJS应用程序的前端向后端发送一个字符串

在服务器端,我的代码如下所示:

app.post('/mydb/post', function(req, res, next) {

console.log(req.body);

});
应通过以下方式发送字符串:

var http = new XMLHttpRequest();
var url = '/mydb/post';
var params = 'john';
http.open('POST', url, true);

//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'text/plain');

http.onload = function () {
  // do something to response
  console.log(this.responseText);
};
http.send(params);
但是请求的主体似乎是空的

也许你知道一些事情,那会有帮助

亲切问候,,
mg

您必须更改contentType
用这个

函数requestUtils(方法、url、正文){
var xhr=new XMLHttpRequest();
open(方法、url、true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.onreadystatechange=函数(){
if(this.readyState==XMLHttpRequest.DONE&&this.status==200){
console.log(this);
log(url,body);
}
}
xhr.send(body);
}
requestUtils('post','/mydb/post','name=john')

尝试将
内容类型
标题值设置为
application/x-www-form-urlencoded
我先这样做,然后尝试文本,但使用
expressjs
时的结果相同