我正在尝试通过 JavaScript 将 XML 发布到 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**);
希望这是有道理的。
最佳答案
您可以将 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>\
");
关于javascript - 如何通过 JavaScript 将 XML 发布到 REST API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3507349/