Javascript 如何通过POST将字符串从客户端发送到服务器
我想从NodeJS应用程序的前端向后端发送一个字符串 在服务器端,我的代码如下所示: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
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
时的结果相同