如何在 JavaScript 中发送专门的 XML 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15574719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a specialized XML request in JavaScript
提问by user2161457
I'm fairly new with XML...
我对 XML 还很陌生...
How would I send the following XML to "https://www.exampleserver.com" ?
我如何将以下 XML 发送到“ https://www.exampleserver.com”?
<?xml version='1.0' encoding='UTF-8'?>
<methodCall>
<methodName>ContactService.add</methodName>
<params>
<param>
<value><string>privateKey</string></value>
</param>
<param>
<value><struct>
<member><name>FirstName</name>
<value><string>John</string></value>
</member>
<member><name>LastName</name>
<value><string>Doe</string></value>
</member>
<member><name>Email</name>
<value><string>[email protected]</string></value>
</member>
</struct></value>
</param>
</params>
</methodCall>
回答by CL22
With client sidescripting, you can only send the XML to the same domain as the one the web server is on, unfortunately. This is a security feature. However, you could send it to your own server and have your server send it.
不幸的是,使用客户端脚本,您只能将 XML 发送到与 Web 服务器所在的域相同的域。这是一项安全功能。但是,您可以将它发送到您自己的服务器并让您的服务器发送它。
To send it to your own server, you could do the following:
要将其发送到您自己的服务器,您可以执行以下操作:
var xml = '' +
'<?xml version='1.0' encoding='UTF-8'?>' +
'<methodCall>' +
'<methodName>ContactService.add</methodName>' +
'<params>' +
' <param>' +
' <value><string>privateKey</string></value>' +
' </param>' +
' <param>' +
' <value><struct>' +
' <member><name>FirstName</name>' +
' <value><string>John</string></value>' +
' </member>' +
' <member><name>LastName</name>' +
' <value><string>Doe</string></value>' +
' </member>' +
' <member><name>Email</name>' +
' <value><string>[email protected]</string></value>' +
' </member>' +
' </struct></value>' +
' </param>' +
'</params>' +
'</methodCall>';
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","https://www.yourdomain.com/thepage",true);
xmlhttp.send(escape(xml));