我已經休息呼叫,在那裏它需要一個參數EMPID傳遞類型如何消耗POST調用,其中參數是類型的應用/ X WWW的窗體-urlencoded在節點JS
application/x-www-form-urlencoded
蔭試圖消耗,使用以下code.But值越來越爲未定義傳遞給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) ;
}
請能有人指出錯誤,並幫助我如何以正確的格式發佈。
***編輯添加的服務器代碼
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);
});
});
https://www.npmjs.com/package/ body-parser你需要添加它來解析正文數據。希望這可以幫助。 –