简体   繁体   中英

Posting XML data to a PHP API via Phonegap/Ajax

I've been task with building an app to check on the status of servers through a phonegap app. The trouble is the client is not giving me access to the existing API or the server. The only information he has given me is: "You can send a POST request to the xmlUserApi.php named "request" by jQuery for example."

As I understand it, we are sending some XML in the format

<xmlApi>
  <action> getServerList </action>
   <auth></auth>
</xmlApi>

For example, from which a an XML list of all the servers is returned.

Whenever I try to POST this data to the PHP (xmlUserApi.php), nothing is returned. I feel it would be helpful to look through the PHP, but, the client won't let me.

Any help/ideas would be really appreciated

EDIT

The response I'm getting in the inspector is :

<form action=xmlUserApi.php method=post>
<textarea name=request cols=120 rows=30></textarea>
<input type=submit value=Request></form><br><br>86.135.213.213

What your client is suggesting is programmatically replicating the action of a user filling out the form at the address they provided.

To do that, try this:

var xmlData = '<xmlApi><action> getServerList </action><auth>xxxxxx</auth></xmlApi>';
var serverPath = 'http://some.com/path/';
var requestPath = 'xmlUserApi.php';
var request = $.ajax({
    url: requestPath,
    type: 'POST',
    dataType: 'jsonp',
    data: { request: xmlData }
});
request.done(function(data) {
    /process data
});
request.fail(function(jqXHR, textStatus) {
    alert(textStatus);
});

//Leaving original answer down here

"You can send a POST request to the xmlUserApi.php named "request" by jQuery for example."

Not sure what they mean by 'named "request"', but with jQuery you would do this:

var serverPath: 'http://their.server.tld/path/';
var authToken: 'sometoken';
$.post(serverPath + 'xmlUserApi.php', { action: 'getServerList', auth: authToken },
   function(data) {
      //whatever you want to do with the return goes here
   }
);

There may also be an issue with the connection to the API, sometimes passing additional parameters helps with this, you could try:

var serverPath = 'http://their.server.tld/path/';
var requestPath = serverPath + 'xmlUserApi.php';
var authToken = 'sometoken';
$.ajax({
    url: requestPath,
    type: 'POST',
    dataType: 'jsonp',
    data: { action: 'getServerList', auth: authToken }
}).done(function(data) {
    //processing here
}).fail(function(jqXHR, textStatus) {
    alert(textStatus);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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