Javascript 类型错误:a 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13902782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:05:45  来源:igfitidea点击:

TypeError: a is undefined

javascriptjquerytypeerror

提问by devmonster

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

当我尝试使用该$.each方法时,我在 jQuery v1.7.2 代码中收到上述错误:

$.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?

为什么我会收到这个错误?

回答by Bob Fanger

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.

您所做的某些事情导致了 jQuery 中的错误。在 99.9% 的情况下,这是代码中的错误,而不是 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.

使用 jQuery 的开发版本会有所帮助。它不是过压缩,这意味着它仍然有充分的变量名来代替ab等等。

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 $.eachto confirm the data looks like you'd expect.

之前echo json_encode($data);?这将触发 jQuery 的 JSON 检测。在console.log(data);之前添加$.each以确认数据看起来像您期望的那样。