简体   繁体   中英

How to take the output from web form and POST it to an XML API?

I am trying to take information given in a web form (HTML, JS or other) and use this information to populate an XML document that is then submitted to an API all on the client side.

As simple as I thought this sounds, I am stuck I have been through the best option I have so far is for JS to populate an XML document using variables but I am not sure how to submit the document. This is slightly unusual as it is all hosted locally so could make use of other things if that would work, then need to present the returned XML.

Any help would be greatly appreciated.

Many thanks

Check out this great reference for working with XML in JavaScript:

AJAX and XML handling in JavaScript

You can create an XMLDocument object, and then serialize it to a string so it can be submitted to the API.

Edit 1

To serialize an XMLDocument in IE8 and earlier, use the .xml property .

Edit 2

Once you have your serialized XML document as a string, you can place it into a <form> so it can be submitted to the server. Here's one way of doing it:

HTML

<form action="mypage.php" method="post">
    <input type="hidden" name="xmldata" id="hiddenXMLInput">
    <input type="Submit" value="Submit XML to server">
</form>

JavaScript

document.getElementById('hiddenXMLInput').value = strXMLDocument;

where strXMLDocument is your serialized XML document as a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM