html - Javascript REST API Post Form -


i new rest , trying add variable json body can have input form values.

i have tried using getelementid along html form after wrapping the script in function, having no success.

    var requestbody = "{'companyid':'getelementbyid('companyid')','knowledgeid':'getelementbyid('knowledgeid')','source_code':'getelementbyid('source')','article_title':'getelementbyid('title')'}";   var client=new xmlhttprequest(); client.open("post","<url>");  client.setrequestheader('accept','application/json');  client.setrequestheader('content-type','application/json');  client.setrequestheader('authorization', 'basic '+btoa('admin'+':'+'admin'));  client.onreadystatechange = function() {      if(this.readystate = this.done) {         document.getelementbyid("response").innerhtml=this.status + this.response;      }  };  client.send(requestbody); 

here form:

<form enctype="application/json" action="<url>" method="post"> title:<input id="title" type="text"></input> company:<input id="companyid" type="text"></input> knowledgeid:<input id="knowledgeid" type="text"></input> html:<input id="source" type="text" ></input>   <input type="button" value="send" onclick="restrequest()"></input> 

when defining value of requestbody don't want element itself, it's value. have single quoted it's not going evaluated.

var requestbody = "{'companyid': '" + document.getelementbyid('companyid').value + "', 'knowledgeid': '" + document.getelementbyid('knowledgeid').value + "', 'source_code': '" + document.getelementbyid('source').value + "', 'article_title': '" + document.getelementbyid('title').value + "'}"; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -