[英]How to consume POST call where parameter is of type application/x-www-form-urlencoded in node js
I have a rest call,where it requires one parameter empid to be passed of type 我有一个rest呼叫,它需要一个参数empid传递类型
application/x-www-form-urlencoded
Iam trying to consume that using the following code.But the value is getting passed as undefined to the restcall 我正在尝试使用以下代码来使用它,但是该值正以未定义的形式传递给restcall
function searchEmployee(employeeid){
var empEndpoint = 'http://localhost:3001/searchemp';
var http = new XMLHttpRequest();
http.open('POST',empEndpoint,true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
var empid = employeeid;
http.send(empid) ;
}
Please can someone point out the mistake and help me how to POST that in proper format. 请有人指出错误并帮助我如何以正确的格式发布它。
***Edit added server code ***编辑添加的服务器代码
app.post('/searchemp',function(req,res){
reqbody = req.body.empid;
console.log(reqbody,"reqbody");
var empdetails =con.query('SELECT * FROM tasktable WHERE Empid =?',reqbody,
function(err,rows){
if(err) throw err;
console.log('EMP Data received from Db:\n');
res.send(rows);
});
});
That depends on where you call the function searchEmployee
from and how you do it. 这取决于您从何处调用函数
searchEmployee
以及如何执行。
as var empid = employeeid;
如
var empid = employeeid;
and employeeid
is the first parameter of your function you should call searchEmployee
with an appropriate parameter such as 2474
. 并且
employeeid
是函数的第一个参数,应使用适当的参数(例如2474
调用searchEmployee
。
so your should call it like that: 所以您应该这样称呼它:
searchEmployee(2474); function searchEmployee(employeeid){ var empEndpoint = 'http://localhost:3001/searchemp'; var http = new XMLHttpRequest(); http.open('POST',empEndpoint,true); http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); var params = `empid=${empid}`; http.send(params); }
maybe this helps: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php 也许这会有所帮助: http : //www.openjs.com/articles/ajax_xmlhttp_using_post.php
var url = "get_data.php"; var params = "lorem=ipsum&name=binny"; http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(params);
especially this should be the way to go: 特别是应该这样:
var params = "lorem=ipsum&name=binny";
your equivalent should look like this: 您的等效项应如下所示:
var params = `empid=${empid}`; http.send(params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.