I’m trying hit the API over jquery ajax with the URL and parameter with post method. here I’m checking delivery is available or not for the given Pincode for one of e-com website,please help me out on how to check or receive data from the API response.
My Script :
<script type="text/javascript">
$(document).ready(function () {
$('#check').click(function(){
var api_url = 'http://example.com/webservices/b2cpincodecheck.jsp';
var key = '4A8DA5E635656';
var pincode = 604408 ;
if(pincode=="") {
alert("please enter the pincode");
} else {
$.ajax({
url:api_url+"?reqid="+key+"&pincode="+pincode,
dataType: "text/json",
type: "POST",
success: function(result){
//console.log(result);
if(result) {
alert("Delivery is available!");
} else {
alert("Delivery is not available!");
}
}
})
}
});
});
</script>
As per the API response in the given document, I would get data in XML format
Their response :
<response>
<result>successful</result>
<pinCode>604408</pinCode>
<ouCode>abc</ouCode>
</response>
Please help me out on how to check or receive data from the API response.
Thanks in advance.
2
Answers
jQuery AJAX
Method is to load data from external website by calling APIs, and get the response inJSON
orXML
formats. this example shows you how easy it is to make such API calls injQuery AJAX
.something like thisTo get result
<div id="message"></div>
Here is two example of data extraction from xml :
loadData
get exaclty the field you expectxmlToObject
can help you to walk an XML as if it was a simple JavaScript object.Please be careful: jQuery ajax setting
dataType
should be empty or the direct format (ie:"json"
, not"text/json"
), here you should set it to"xml"
.Hope it will help you!