简体   繁体   中英

How To Post XML via JavaScript to REST API?

I'm trying to POST XML via JavaScript to a REST API.

The Request Data looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EditGame xmlns="http://blahblahblah.com" >
<playerCount>2</playerCount>
<score>2621440</score>
</EditGame>

How do I define the postString above if my code looks like this below:

xhr.open('POST',URLgameUpdateAction);
xhr.setRequestHeader('Content-type','application/x-www.form-urlencoded');
xhr.send(**postString**);

Hope this makes sense.

You can pass the XML as a simple string.

xhr.open('POST',URLgameUpdateAction);
xhr.setRequestHeader('Content-type','application/x-www.form-urlencoded');
xhr.send("\
  <?xml version='1.0' encoding='UTF-8' standalone='yes'?>\
  <EditGame xmlns='http://blahblahblah.com'>\
  <playerCount>2</playerCount>\
  <score>2621440</score>\
  </EditGame>\
");

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