简体   繁体   English

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

[英]How to Send an XML Data Stream via a HTTP Post Request

So I need to send an XML data stream via a http post request.所以我需要通过 http post 请求发送一个 XML 数据流。 I'm finishing my first app and trying to add an sms sending capability to it.我正在完成我的第一个应用程序并尝试为其添加短信发送功能。 I need to send an xml file like this:我需要发送一个这样的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>

to a http address: https://www.blablasms.cm/api到一个 http 地址: https : //www.blablasms.cm/api

I'd like to send it as part of a click event, like this for example:我想将它作为点击事件的一部分发送,例如:

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

How can it be done?怎么做到呢? Can I do it with an ajax request?我可以用ajax请求来做吗? What would be a valid ajax request for that kind of use?对于这种用途,什么是有效的 ajax 请求? Can I send the xml data as part of the url?我可以将 xml 数据作为 url 的一部分发送吗?

Thanks!谢谢!

 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>

Here you are, tested and it works like a charm!在这里,经过测试,它就像一个魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM