Patrick
Patrick

Reputation: 355

getJSON callback is not called

I've been working on a minecraft status bar for my site, and I have written up this code using my minimal knowledge of Javascript and jQuery. From what I've read it seems like it should be working fine.

var url = 'minecove.org'; // URL of Server to Query
var port = '25565'; // Port of the Server
$.getJSON('http://api.syfaro.net/minecraft/1.2/server/status?ip=' + url + '&port=' + port), function(data) {
    var online = data.online;
    var playerson = data.players.online;
    var maxplayers = data.players.max;
    if (online) {
        $('#online').text('Online').css('color', '#000000');
        $('#players').text(playerson + '/' + maxplayers + ' players online');
    }
    else {
        $('#online').text('Offline').css('color', '#333333');
        $('#players').text('No Players');
    }
}

I have two divs on the page, "online" and navigating to http://api.syfaro.net/minecraft/1.2/server/status?ip=minecove.org&port=25565 works fine, returning the JSON data. Am I just coding it wrong? According to the several tutorials I've read through this approach should work. I've also changed the JSON line to be $.getJSON(http://api.syfaro.net/minecraft/1.2/server/status?ip=minecove.org) to eliminate any errors in the variables above, but that doesn't work either. I would really appreciate some help with this.

Thanks.

Upvotes: 1

Views: 332

Answers (3)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

You have 2 semantic errors. One is closing a parenthesis prematurely, and the other is not closing the final parenthesis after the function. Your code should be:

var url = 'minecove.org'; // URL of Server to Query
var port = '25565'; // Port of the Server
$.getJSON('http://api.syfaro.net/minecraft/1.2/server/status?ip=' + url + '&port=' + port, function(data) {
    var online = data.online;
    var playerson = data.players.online;
    var maxplayers = data.players.max;
    if (online) {
        $('#online').text('Online').css('color', '#000000');
        $('#players').text(playerson + '/' + maxplayers + ' players online');
    }
    else {
        $('#online').text('Offline').css('color', '#333333');
        $('#players').text('No Players');
    }
});

I tested it and it's working fine.

Cheers, from La Paz, Bolivia

Upvotes: 0

TastySpaceApple
TastySpaceApple

Reputation: 3185

the function getJSON takes a function as an argument.

http://api.jquery.com/jquery.getjson/

var url = 'minecove.org'; // URL of Server to Query
var port = '25565'; // Port of the Server
$.getJSON('http://api.syfaro.net/minecraft/1.2/server/status?ip=' + url + '&port=' + port/*)*/, function(data) {
    var online = data.online;
    var playerson = data.players.online;
    var maxplayers = data.players.max;
    if (online) {
        $('#online').text('Online').css('color', '#000000');
        $('#players').text(playerson + '/' + maxplayers + ' players online');
    }
    else {
        $('#online').text('Offline').css('color', '#333333');
        $('#players').text('No Players');
    }
});

Upvotes: 0

user2864740
user2864740

Reputation: 61975

You are being tricked by the comma operator (which is tricky because the result is still valid syntax).

Does something look wrong with the following?

$.getJSON('x' + url + '&port=' + port), function(data) {
   //..
}

It should. The parenthesis after the port should, in fact, be after the callback so that it is also passed as an argument.

$.getJSON('x' + url + '&port=' + port, function(data) {
   //..
})

Upvotes: 4

Related Questions