如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:14:27  来源:igfitidea点击:

How to send a specialized XML request in JavaScript

javascriptxmlxml-rpc

提问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));