user2161457
user2161457

Reputation: 61

How to send a specialized XML request in JavaScript

I'm fairly new with XML...

How would I send the following XML to "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>

Upvotes: 1

Views: 13113

Answers (1)

Jayy
Jayy

Reputation: 14798

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

Upvotes: 6

Related Questions