$.ajax working with file and not url?
Question
in the $.ajax function the url part has data.json which is a text file but i want to put a url i.e.
the code works with
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'data.json',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
where data.json is a text file...but wen i replace 'data.json' with 'http://twittercounter.com/api/username=Anand_Dasgupta&output=json&results=3'...which is the actual url ,then there is no output...
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'http://twittercounter.com/api/username=Anand_Dasgupta&output=json&results=3',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
an advice will be highly appreciated. Thank You.
Solution
As @harshath.jr correctly pointed out, you will need to proxy that request through your domain, for example:
$('#content').html('');
$.ajax({
url:'twitterProxy.php?username=Anand_Dasgupta&results=3',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
OTHER TIPS
This seems to be a case of cross domain ajax prevention.
You will need to use a server side proxy script for this.
using $.getJSON should solve all your worries. And it'll call you in the morning.
Seems you have typo in url, the question mark is significant since it differentiate url from parameters:
http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3
As for me looks like missing some more parameters.
EDIT:
The below answers looks more correct than mine, it definitely could be cross domain access.