-1

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});
11
  • Navigate directly to the url+ip (using your web browser) and post what is returned here. Commented Dec 19, 2013 at 15:08
  • Does url directly in browser work? what does it return? Commented Dec 19, 2013 at 15:09
  • Okay, I'll post the API response. Commented Dec 19, 2013 at 15:09
  • Does adding a second ? to &callback=? help? &callback=?? what version of jquery is this Commented Dec 19, 2013 at 15:12
  • @KevinB Directly like this maybe? api.syfaro.net/minecraft/1.2/server/… Commented Dec 19, 2013 at 15:13

3 Answers 3

2

The response doesn't look like an Array. It is just an object. Try using just json.status, instead of json[0].status.

Sign up to request clarification or add additional context in comments.

Comments

1

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:

http://jsfiddle.net/yj467/1/

$.getJSON( "http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=?", function( json ) {
  console.log( json );
  $(".status").html(json.status);
});

6 Comments

jQuery should be filling that part in for you
Doesn't work. I'm pretty sure it wouldn't even work without a callback.
You want callback=?. jQuery will replace the ? with a generated function name.
@Alyn check my jsfiddle, simplified all it works fine!
@salivan actually worked! I must have been doing something wrong
|
-1

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
}

http://jsfiddle.net/3VZuP/

4 Comments

This is what jquery is doing behind the scenes.
He IS using JSONP. callback=? tells jQuery to do this for you.
This solution won't work, as the response doesn't set a variable. You would be importing a script with an object that isn't assigned to a variable, and therefore have no way to reference it.
@bic incorrect, it would be executing a function with said object as a parameter, jquery defines said function and passes it's name to the callback parameter. The code in this answer will work, but i doubt it has anything to do with the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.