最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to send a specialized XML request in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I'm fairly new with XML...

How would I send the following XML to "" ?

<?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>

I'm fairly new with XML...

How would I send the following XML to "https://www.exampleserver." ?

<?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>
Share Improve this question edited Mar 22, 2013 at 16:05 Matt 3,6872 gold badges28 silver badges40 bronze badges asked Mar 22, 2013 at 15:59 user2161457user2161457 611 gold badge1 silver badge7 bronze badges 7
  • If you know how to send any other kind of data - XML is no exception (as long as you know how to pose the XML - which you seem to). That is, either using XmlHttpRequest or with jQuery.ajax or its equivalent in your favorite library. Are you using a library? – John Dvorak Commented Mar 22, 2013 at 16:02
  • Well that is part of my problem, I've never used XMLHttpRequest extensively before. I've looked into it and I've got this as a starting point: var NewRequest = new XMLHttpRequestMethod(); NewRequest.open("POST","exampleserver."); NewRequest.send(key,data); where "key" is a predefined variable and "data" is an object array of values. (But the issue is defining the "method name" in that data. – user2161457 Commented Mar 22, 2013 at 16:17
  • To clarify, I'm having trouble defining the XML "methodName" as "ContactService.add" as required by the API I'm working with – user2161457 Commented Mar 22, 2013 at 16:25
  • Your XML seems valid as an XML. What is the required format? – John Dvorak Commented Mar 22, 2013 at 16:37
  • It also looks like valid XML-RPC request as per the wikipedia example – John Dvorak Commented Mar 22, 2013 at 16:40
 |  Show 2 more ments

1 Answer 1

Reset to default 6

With client side scripting, 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.

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./thepage",true);
xmlhttp.send(escape(xml));
发布评论

评论列表(0)

  1. 暂无评论