devmonster
devmonster

Reputation: 1739

TypeError: a is undefined

I get the above error on the jQuery v1.7.2 code when I try to use the $.each method:

$.post('url_of_php_file.php',
            $.param( {

            }),
            function(data){
                $.each(data.articles, function(index, value){
                .....
});

The request returns:

{"articles": [
    {
        "id":"11",
        "date":"2012-12-19 15:52:06",
        "title":"url_title",
        "link":"url_link",
        "available":"1"
     },
    ..... *more rows like the above*
]}

Why do I get this error?

Upvotes: 9

Views: 35732

Answers (1)

Bob Fanger
Bob Fanger

Reputation: 29897

Something you did caused an error inside jQuery. This is 99.9% of the time a bug in your code and not a bug in jQuery.

What helps is using the development version of jQuery. It is not minified, which means that it still has the full variable names instead of a, b, etc.

Did you send a:

header('Content-Type: application/json');

before the echo json_encode($data);? That would triggers jQuery's JSON detection. Add a console.log(data); before the $.each to confirm the data looks like you'd expect.

Upvotes: 13

Related Questions