Reputation: 403
I'm trying to make an API call to a Minecraft api that returns, along others, a status message, and then print it out on the site. For that I'm using an AJAX JSON call, getting the entered string to connect to the API and to GET the information about the server in a function (which I'm pretty sure is the most common way to do that).
However, when I try and enter a (working) IP, it displays the researching data... message very briefly and then clears the field again. Nothing appears, even though there should be a status massage from the API. It doesn't even log in the console.
Any help on that?
JS Code:
function getStatus(){
var ip = $("#ip").val(); //getting the value from the textbox
if(ip == ""){
$("#addmes").html("<h1>you didn't enter anything -.-</h1>"); //checking if anything is entered
} else {
$("#addmes").html("<h1>researching data...</h1>"); //displaying a temporary status message
$.getJSON("http://api.syfaro.net/minecraft/1.2/server/status?ip="+ ip + "&callback=?", function(json){ //connecting to the API, using the IP variable in the process
console.log(json); //console log
$("#addmes").html("<h1>" + json[0].status + "</h1>"); //printing out the API status message
})
}
}
API response (tested with http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=?):
?({"status":"success","ip":"mc.hypixel.net","port":25565,"last_update":"2013-12-19 09:09:58","online":true,"motd":"\u00a7aHypixel Lobby \u00a76| \u00a7cPlay Now! \u00a7eMega Walls \u00a7aPublic BETA!","players":{"max":16001,"online":6989,"list":false},"version":"1.7.2","favicon":false});
Upvotes: -1
Views: 1096
Reputation: 3312
Edit: I read up on the documentation of getJSON and it seems it does automatically recognize JSONP requests. Leaving the post here in case it's helpful to someone
I suspect you're being scuppered by the Same-origin policy.
If you're (as I suspect) not actually the programmer of syfaro.net and you're not running this script on that server, you won't be able to access the page.
Helpfully, to get around this people created JSONP, which is a rather nasty hack it its way but will let you do what you're after.
You can use it like this:
function getStatus(){
var ip = $("#ip").val(); //getting the value from the textbox
if(ip == ""){
$("#addmes").html("<h1>you didn't enter anything -.-</h1>"); //checking if anything is entered
} else {
$("#addmes").html("<h1>researching data...</h1>"); //displaying a temporary status message
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://api.syfaro.net/minecraft/1.2/server/status?ip="+ ip + "&callback=jsonCallback';
head.appendChild(script);
}
}
function jsonCallback(json){
$("#addmes").html("<h1>" + json[0].status + "</h1>"); //printing out the API status message
}
Upvotes: -1
Reputation:
Try this url:
http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=jsonp
Your original url with just ? returns without function name in the beginning. I'm not sure if jsonp without function name is possible or not, but you can check...
It works here:
$.getJSON( "http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=?", function( json ) {
console.log( json );
$(".status").html(json.status);
});
Upvotes: 1
Reputation: 3134
The response doesn't look like an Array. It is just an object. Try using just json.status, instead of json[0].status.
Upvotes: 2