Calling an ASMX Web Service From Other Server Using jQuery and PHP
Introduction
A few months back, I got a requirement to fetch some inventory data from a server and show it to an HTML page. The interesting fact was the client server (the one that I want to show the data in) does not support .Net files. If did support them then we could directly add the web references and display the data. So what to do? I tried JSONP and Angular JSON but I could not make it to work. Then I came up with an idea.
Here, I have:
Using the code
So let’s start
Make an HTML table in the file like the following:
[html]
<table id="“inventory”">
<tbody>
<tr id="“maintr”">
<th>LOCATION</th>
<th>20 GP</th>
<th>40 FC</th>
<th>40 GP</th>
<th>40 OT</th>
<th>40 HCGP</th>
</tr>
</tbody>
</table>
[/html]
Add the JQuery reference.
[js]
<script src="“js/jquery-1.9.1.js”" type="“text/javascript”"></script>Add the styles to the table:
[/js]
Add the following Scripts to get the data from the web service:
[js]
<script type="“text/javascript”">// <![CDATA[
var gp20;
var gp40;
var fc40;
var ot40;
var hcgp40;
$(document).ready(function () {
$(“#accordion”).accordion();
$.post(“RetWS.php”, {
action: “test”
},
function (data) {
var className;
var i = 0;
var xdoc = $.parseXML(data),
$xml = $(xdoc);
$($xml).find(‘AvailableSaleUnitsDetail’).each(function () {
if (i % 2 != 0) {
className = ‘alt’;
}
else {
className = ‘normal’;
}
if (parseInt($(this).find(‘_x0032_0GP’).text()) > 50) {
gp20 = ’50 +’;
}
else {
gp20 = $(this).find(‘_x0032_0GP’).text();
}
if (parseInt($(this).find(‘_x0034_0FC’).text()) > 50) {
fc40 = ’50 +’;
}
else {
fc40 = $(this).find(‘_x0034_0FC’).text();
}
if (parseInt($(this).find(‘_x0034_0GP’).text()) > 50) {
gp40 = ’50 +’;
}
else {
gp40 = $(this).find(‘_x0034_0GP’).text();
}
if (parseInt($(this).find(‘_x0034_0OT’).text()) > 50) {
ot40 = ’50 +’;
}
else {
ot40 = $(this).find(‘_x0034_0OT’).text();
}
if (parseInt($(this).find(‘_x0034_0HCGP’).text()) > 50) {
hcgp40 = ’50 +’;
}
else {
hcgp40 = $(this).find(‘_x0034_0HCGP’).text();
}
$(“#inventory”).append(“
<tr class=” + className + ” >
<td id=” + $(this).find(‘Location’).text() + “>” + $(this).find(‘Location’).text() + “</td>
<td>” + gp20 + “</td>
<td >” + fc40 + “</td>
<td>” + gp40 + “</td>
<td >” + ot40 + “</td>
<td >” + hcgp40+ “</td>
</tr>
”); i++;
});
})
});
// ]]>
</script>
[/js]
Here, RetWS.php is my PHP file that actually gets the data from an asmx web service from another server.
In the RetWs.php you can do the following codes:
[php]
<!–?php if(isset($_POST[‘action’]) && !emptyempty($_POST[‘action’])) { $action = $_POST[‘action’]; switch($action) { case ‘test’ : doWebService(); break; } } function doWebService() { $client = new SoapClient(“http://Here paste link to your webservice”); echo $client->getInventoryForWs()->getInventoryForWsResult;<br ?–> }
?>
[/php]
The link may look like:
http://www.something.com/Reports/Marketing/Mywebservice.asmx?WSDL
Make sure that there are no spelling mistakes.
Kindest Regards
Sibeesh Vennu