Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:;无法读取未定义的属性_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 未捕获类型错误:;无法读取未定义的属性

Javascript 未捕获类型错误:;无法读取未定义的属性,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用ajax创建了一个动态内容: $(function(){ $('#loadFeed').bind('click',function(){ $.getJSON('getData.php', function(json) { var output="<ul id='feedsList'>"; for(var i=json.posts.length-1;i>=json.posts.length-31;i-

我使用ajax创建了一个动态内容:

$(function(){
   $('#loadFeed').bind('click',function(){ 
        $.getJSON('getData.php', function(json) {
            var output="<ul id='feedsList'>";

            for(var i=json.posts.length-1;i>=json.posts.length-31;i--){
                output+="<li class='post'>";
                output+="<div class='text' id='"+json.posts[i].id+"'>"+json.posts[i].shortmsg+"</div>";         
                output+="</li>";
            }
            output+="</ul>"
            $(output).appendTo('.posts');
    });
  });
});
我不知道为什么它会将msg识别为未定义。我在getData.php中定义了msg,就像我对id shortmsg所做的一样。这是我的getData.php:

<?php

        // Connecting, selecting database
        $link = mysql_connect('localhost', 'root')
            or die('Could not connect: ' . mysql_error());
        mysql_select_db('my_db') or die('Could not select database');

        // Get Data
        $query = 'SELECT * FROM feed ORDER BY feed.userID ASC';

        $result = mysql_query($query) or die('Query failed: ' . mysql_error());

        //create json format text from posts
        $json=array();
        while($row=mysql_fetch_array($result)){
            $post=array('id'=>$row['userID'],'avatar'=>$row['avatar'],'postType'=>$row['postType'],'msg'=>$row['msg'],'shortmsg'=>$row['shortmsg'],'title'=>$row['title'],'type'=>$row['type'],'pic'=>$row['pic'],'link'=>$row['link']);
            array_push($json,$post);
        }

        //display it to the user
        header('Content-type: application/json');
        echo "{\"posts\":".json_encode($json)."}";

        // Closing connection
        mysql_close($link);

        return $row;

?>


谢谢

json响应是什么?在成功回调中尝试
console.log(json)
。我们必须看到php和/或json,因为这是json响应格式的问题。它没有您期望的
msg
属性。@Vega谢谢!我知道错误发生的原因。json响应显示post[102],但thisID-1可能大于102@但是为什么帖子的长度会小于最大id?
<script>
$(".posts").on("click", ".text",function(){
        var thisID = this.id; 
        $.getJSON('getData.php', function(json) {  
            alert(thisID);
            $(this).replaceWith("<div class='long_text'>"+json.posts[thisID-1].msg+"<div>"); 
        });
});
</script>
Uncaught TypeError: Cannot read property 'msg' of undefined .
<?php

        // Connecting, selecting database
        $link = mysql_connect('localhost', 'root')
            or die('Could not connect: ' . mysql_error());
        mysql_select_db('my_db') or die('Could not select database');

        // Get Data
        $query = 'SELECT * FROM feed ORDER BY feed.userID ASC';

        $result = mysql_query($query) or die('Query failed: ' . mysql_error());

        //create json format text from posts
        $json=array();
        while($row=mysql_fetch_array($result)){
            $post=array('id'=>$row['userID'],'avatar'=>$row['avatar'],'postType'=>$row['postType'],'msg'=>$row['msg'],'shortmsg'=>$row['shortmsg'],'title'=>$row['title'],'type'=>$row['type'],'pic'=>$row['pic'],'link'=>$row['link']);
            array_push($json,$post);
        }

        //display it to the user
        header('Content-type: application/json');
        echo "{\"posts\":".json_encode($json)."}";

        // Closing connection
        mysql_close($link);

        return $row;

?>