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
Post a Comment