2010-08-17 90 views
3

我想通過JavaScript將XML POST到一個REST API。如何將XML通過JavaScript發佈到REST API?

請求數據是這樣的:

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

如何定義上述postString如果我的代碼看起來像下面這樣:

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

希望這是有道理的。

回答

1

您可以將XML作爲簡單字符串傳遞。

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>\ 
");