views:

24

answers:

1

Hi there.

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.

A: 

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'&gt;\
  <playerCount>2</playerCount>\
  <score>2621440</score>\
  </EditGame>\
");
mhitza