2010-08-17 49 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>\ 
"); 
相关问题