3

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.

1 Answer 1

1

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>\
");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.