Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 289
devmonster Avatar asked Dec 16 '12 15:12

devmonster


1 Answers

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.

like image 107
Bob Fanger Avatar answered Oct 07 '22 04:10

Bob Fanger