I have the following code :
var _07votes = $("#07votes");
$(document).ready(function() {
setInterval(function() {
$.ajax({
url: "http://services.runescape.com/m=poll/retro_ajax_result?callback=?",
method: "GET",
contentType: "application/json",
dataType: "jsonp",
timeout: 10000;
}).done(function(data){
if (data.votes > 0) {
_07votes.text("" + data.votes);
}
});
}, 3000);
});
I can't get this Ajax code to work, it's like it won't query. Would anyone know why?
You are getting the reference to the element where you put the result before it exists. Move it inside the ready
event handler:
$(document).ready(function() {
var _07votes = $("#07votes");
...
Also, remove the semicolon after the timeout value:
timeout: 10000
In addition to Guffa's answer, you must first parse the JSON-response (when you succeed to retrieve it).
You can do this using the jQuery.parseJSON()
-method.