Again+Updated: This jQuery code doesn't work, Why?
I asked this morning a question about a jQuery code that's not working (which grabs a json file using API) then some great people told that I must have the json file in jsonp to use callback , which I wasn't have..
I communicated with the developer of the website that provides the API and he solved the problem so that it supports jsonp, but still the code doesn't work !!
here is the code of the page (you can see it viewing the source of this page http://rawaji.com/test.html )
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$.getJSON("http://api.qaym.com/0.1/countries/key=u2gTerCTZDudTCMhQBQ9&jsoncallback=?",
function(data){
$.each(data, function(i,item){
$("#results").append("<span class='result' >" + item.name + "</span></br>");
if ( i == 3 ) return false;
});
});
});
</script>
</head>
<body>
<div id="results">
</div>
</body>
</html>
just a little note, tha开发者_Python百科t FireBug tells me no errors in the code and even the console tells that the file imported correctly (see the image here http://twitpic.com/5yycg5 )
WHAT IS MISSING !! :(
UPDAATE:
I used another consoler and it shows me this message!
key=u2gTerCTZDudTCMhQBQ9&jsoncallback=jQuery16206894429267849773_1312140242381:-1 Resource interpreted as Script but transferred with MIME type application/json.
I just know nothing about it, but I will search it. if someone has an idea, plz tell :)
This works:
$(function(){
$.ajax({
url: 'http://api.qaym.com/0.1/countries/key=u2gTerCTZDudTCMhQBQ9',
dataType: 'jsonp',
crossDomain:true,
success:function(data){
$.each(data, function(i,item){
$("#results").append("<span class='result' >" + item.name + "</span></br>");
if ( i == 3 ) return false;
});
},
error:function(jqxhr,err,status){
alert(status);
}
});
});
The 401 error coming back requires authentication - invalid API key.
http://jsfiddle.net/AlienWebguy/CqrPe/
精彩评论