I want to get the api values using json. if I click the json button I did not get any response I don't know why this is not running yesterday I have checked with api method it is in post only.I don't know where i am lacking. Here is my code:
<script type="text/javascript">
function json()
{
xmlhttp= new XMLHttpRequest();
var url="http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&username=ram@demo.com&password=newnew&devicemedium=WEB";
alert(url);
//var url="dbarr.php";
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var ret_arr=JSON.parse(xmlhttp.responseText);
json_arr(ret_arr);
}
}
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
function json_arr(x)
{
var res="";
var i;
for(i=0;i<x.length;i++)
{
res+=x[i].name+" "+x[i].mobile+"</br>";
}
document.getElementById('print').innerHTML=res;
}
</script>
<form name="f1" action="" method="post">
<input type="submit" onClick="json();" value="Json">
<p id="print"></p>
</form>
i can suppose what "http://new.ezeeinfosolutions.com" isnt your domain and you nead create some php mirror file on your server.
This file will get response from http://new.ezeeinfosolutions.com and return json.
their can few possible solution, if any of them get you to success than chear up :)
try to set response type.
xmlhttp.responseType = 'json';
try to use xmlhttp.response instead of xmlhttp.responseText
use this example to compare.
var getJSON = function(url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined'
? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
var status;
var data;
// http://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
if (xhr.readyState == 4) { // `DONE`
status = xhr.status;
if (status == 200) {
successHandler && successHandler(xhr.response);
} else {
errorHandler && errorHandler(status);
}
}
};
xhr.send();
};
getJSON('https://mathiasbynens.be/demo/ip', function(data) {
alert('Your public IP address is: ' + data.ip);
}, function(status) {
alert('Something went wrong.');
});
Read More:https://mathiasbynens.be/notes/xhr-responsetype-json