如何通过HTTP Post请求发送XML数据流

时间:2017-10-01 11:26:36

标签: javascript php jquery html

所以我需要通过http post请求发送XML数据流。我正在完成我的第一个应用程序并尝试添加短信发送功能。我需要发送一个像这样的xml文件:

<sms>
<user> <username>Leeroy</username> <password>Jenkins</password>
</user> <source>000</source>
<destinations>
<phone id="external id1">5xxxxxxxx</phone> <phone id="external id2">5xxxxxxxx</phone> <phone>5xxxxxxxx</phone>
<phone id="">5xxxxxxxx</phone>
</destinations>
<message>This is a message</message>
<timing>30/03/14 10:10</timing>
<response>0</response>
</sms>

到http地址: https://www.blablasms.cm/api

我想将其作为点击事件的一部分发送,例如:

$('input').click(function () {
...
...
...
// make an http post request...

怎么做?我可以用ajax请求吗?什么是有效的ajax请求用于这种用途?我可以将xml数据作为网址的一部分发送吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

var pathToPost = 'https://requestb.in/17x6jwi1'; // example path

$.ajax({
  method: 'POST',
  url: pathToPost,
  data: {
    user: {
      username: document.getElementsByTagName('username')[0].textContent,
      password: document.getElementsByTagName('password')[0].textContent,
    },
    source: document.getElementsByTagName('source')[0].textContent,
    destinations: {
      phone1: document.getElementById('external_id1').textContent,
      phone2: document.getElementById('external_id2').textContent
    },
    message: document.getElementsByTagName('message')[0].textContent,
    timing: document.getElementsByTagName('timing')[0].textContent,
    response: document.getElementsByTagName('response')[0].textContent
  }
}).then(function(response) {
  // handle response
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<sms>
  <user>
    <username>Leeroy</username>
    <password>Jenkins</password>
  </user>
  <source>000</source>
  <destinations>
    <phone id="external_id1">5xxxxxxxx</phone>
    <phone id="external_id2">5xxxxxxxx</phone>
  </destinations>
  <message>This is a message</message>
  <timing>30/03/14 10:10</timing>
  <response>0</response>
</sms>

在这里,经过测试,它就像一个魅力!