Jquery json函数不能正常工作

jquery json function not working properly

本文关键字:工作 常工作 json 函数 不能 Jquery      更新时间:2023-09-26

这个jquery函数发送一个变量并从php文件中检索数据

function update(){  
    var nid = $("#identity").val(); 
    //alert(nid);
    $.getJSON('../js_backend/getComment.php', {n:nid},function(data){
        $("#comment_view_spot").empty();    
        $.each(data.result, function(){
            $("#comment_view_spot").append("<tr><td>"+this['username']+":</td><td>"+this['comment']+"</td></tr>");  
        });
    }); 
}   

下面是它工作的PHP文件,我的问题是,它工作得很好,当我没有得到我从jquery

发送的变量
$result=array();
//$n=$_GET['n'];
$getJs=$connect->query("SELECT * FROM blog_comment");
while($rows=$getJs->fetch()){
    array_push($result, array(
        'username'=>$rows['username'],
        'comment'=>$rows['comment']
    ));
}
header('Content-type: application/json');
echo json_encode(array("result" => $result));

但是当我获取变量时它不能正常工作如果我试图直接进入PHP文件我看到这一堆代码

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: n in C:'wamp'www'ezoole'js_backend'getComment.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>241728</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:'wamp'www'ezoole'js_backend'getComment.php' bgcolor='#eeeeec'>..'getComment.php<b>:</b>0</td></tr>
</table></font>

请有人帮我解决这个问题。

问题是您正在使用$n=$_GET['n'];,但您使用的方法是post

$n=$_POST['n'];

代替$n=$_GET['n'];

这应该可以正常工作

$.ajax({
url:'../js_backend/getComment.php',
type:'POST',
data:{n:nid},
success: function(data){
$("#comment_view_spot").empty();    
    $.each(data.result, function(){
        $("#comment_view_spot").append("<tr><td>"+this['username']+":</td><td>"+this['comment']+"</td></tr>");
}
});

在服务器端使用

$n=$_POST['n'];