Reputation: 3091
In a different post named "confused how to use jquery getJSON properly" I was having difficulty on how to make my ajax call work and peoples' comments helped me out. But my jquery ajax call still isn't working:
$(document).ready(function(){
$("input#autofill").click(function()
{
$.get("last_year_ISBN.php",
function(data){
alert(data);
var isbns = $.parseJSON(data); //creates a javascript object.
var elements = $('#booklist .ISBN_number');
$.each(isbns, function(index, obj){
alert("hello");
$(elements[index]).val(obj);
});
},
"json");
});
});
The first alert(data) properly prints out the json, so I know the ajax call works. but when I did a debug with firebug, I found out that the variable isbns is set to null. Also, when I put an additional alert("hello") inside the callback function it wasn't called and didn't appear on the screen. Can someone help me figure this out?
Upvotes: 1
Views: 2992
Reputation: 2999
Try using $.getJSON()
This will automatically treats your response as JSON.
More at $.getJSON()
Upvotes: 1
Reputation: 1454
Try running the alerted "json" through a validator like jsonlint.com. If it's not valid json, it will fail to create the object.
Upvotes: 0