1

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?

2 Answers 2

1

Try using $.getJSON() This will automatically treats your response as JSON.

More at $.getJSON()

Sign up to request clarification or add additional context in comments.

Comments

0

Try running the alerted "json" through a validator like jsonlint.com. If it's not valid json, it will fail to create the object.

2 Comments

The json should be valid since I used the php function json_encode to create the result... But I copied the json anyways into jsonlint.com and it didn't work right. It seems that your website only takes up to 60 lines and this is longer... But anyways, do you know why the alert("hello") wouldn't even be called
Older versions of jQuery $.each don't iterate on objects, only arrays. Do you have an array or object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.