Jquery json函数不能正常工作
jquery json function not working properly
这个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'];
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- 最大高度转换不;不工作,工作缓慢
- Gulp-rev:不是第一次工作,而是在第一次工作之后工作
- JQuery - Ajax: encodeUriComponent不工作(EncodeUri工作)