简体   繁体   English

如何在节点js中使用参数为application / x-www-form-urlencoded类型的POST调用

[英]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.

相关问题 如何在+呼叫后内容类型中转义+作为application / x-www-form-urlencoded - How to escape + in post call content type as application/x-www-form-urlencoded 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded 如何使用应用程序 / x-www-form-urlencoded 发送 axios 帖子? - How to send axios post with application / x-www-form-urlencoded? 在JS中,如何在给定URL和application / x-www-form-urlencoded字符串的情况下进行POST? - In JS, how to make a POST given a URL and a application/x-www-form-urlencoded string? node/fastify 不支持的媒体类型出现错误:application/x-www-form-urlencoded - Giving error with node/fastify Unsupported Media Type: application/x-www-form-urlencoded 如何使发布请求具有bas ic auth和内容类型x-www-form-urlencoded - how to make post request baswith ic auth and content type x-www-form-urlencoded 使用内容类型为 application/x-www-form-urlencoded 的 POST Fetch 调用时,从服务器返回的本机 422 不可处理实体错误 - React native 422 Unprocessable Entity error returned from server when using POST Fetch call with content type: application/x-www-form-urlencoded “axios.defaults.headers.common['Content-Type'] = 'application/json'” 但 axios.post 仍然是“application/x-www-form-urlencoded” - "axios.defaults.headers.common['Content-Type'] = 'application/json'" but axios.post still is "application/x-www-form-urlencoded" 如何使用在Content-Type标头中同时具有“ application / x-www-form-urlencoded”和“ charset = UTF-8”的Javascript发布HTML表单 - How to post a HTML form using Javascript that has both “application/x-www-form-urlencoded” and “charset=UTF-8” in the Content-Type header ASP.NET Core提取POST FormData()应用程序/ x-www-form-urlencoded - ASP.NET Core Fetch POST FormData() application/x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM