[英]jquery ajax method not triggering on remote server
首先,我在php,mysql和js上的所有作品都在本地服务器上。这是我第一次将自己的作品发布到网上,但是这样做时遇到了一些问题。
我的网页上有一个后回答部分,我在其中使用jquery ajax提交包含用户答案的表单。此功能在我的本地计算机上运行良好,但是当我尝试在ezphp.tk域下在线提交时 ,我得到以下错误:
SyntaxError:JSON.parse:JSON数据的第1行第1列的意外字符
... tion(b){if(a.JSON && a.JSON.parse)返回a.JSON.parse(b +“”); var c,d = null,e = m.trim(...
我的ajax方法如下:
$.ajax('../includes/verifycomment.php',{
data :{
'comment_body' :$(this).val(),
'userpost_post_id' : <?php echo $postid;?>,
'users_user_id' : <?php echo $userdata->user_id; ?>,
'answers_answer_id':$(this).data('id')
},
datatype:'json',
method :'POST',
success :function(response){
var obj=$.parseJSON(response);
if(obj['text']){
alert(obj['text']);
$(textarea[i]).val('');
$(textarea[i]).hide();
$(comment[i]).show();
return false;
}else{
$(textarea[i]).val('');
$(textarea[i]).hide();
var obj=$.parseJSON(response);
var str="<div class='stylecomment' >"+obj[0]['comment_body']+" --- <span style='color:#d3d3d3;font-size:13px;'> commented by </span><a href='../includes/profile.php?user=<?php echo escape($userdata->username);?>'><?php echo escape($userdata->username);?></a> </div></div>";
$(cmntholder[i]).append(str);
$(comment[i]).show();
}
}
});
我的回复 :
$(this)指的是ajax,在ajax函数外部使用值创建变量,将数据类型更正为dataType,删除对对象的解析并返回,使用preventDefault停止默认的click操作,将url属性附加到ajax对象
尝试:
var cbody = $(this).val(),answ = $(this).data('id');
$.ajax({url:'../includes/verifycomment.php',
data :{
'comment_body' :cbody,
'userpost_post_id' : '<?php echo $postid;?>',
'users_user_id' : '<?php echo $userdata->user_id; ?>',
'answers_answer_id':answ
},
dataType:'json',
method :'POST',
success :function(response){
var obj=response;
if(obj['text']){
alert(obj['text']);
$(textarea[i]).val('');
$(textarea[i]).hide();
$(comment[i]).show();
}else{
$(textarea[i]).val('');
$(textarea[i]).hide();
var obj=response;
var str="<div class='stylecomment' >"+obj[0]['comment_body']+" --- <span style='color:#d3d3d3;font-size:13px;'> commented by </span><a href='../includes/profile.php?user=<?php echo escape($userdata->username);?>'><?php echo escape($userdata->username);?></a> </div></div>";
$(cmntholder[i]).append(str);
$(comment[i]).show();
}
}
});
你不需要这个:
var obj=$.parseJSON(response);
由于您已经具有dataType:"json"
因此您无需再次解析它。
用"dataType:'json',"
替换您的"datatype:'json',"
"dataType:'json',"
示例
$.ajax('../includes/verifycomment.php',{
data :{
'comment_body' :$(this).val(),
'userpost_post_id' : <?php echo $postid;?>,
'users_user_id' : <?php echo $userdata->user_id; ?>,
'answers_answer_id':$(this).data('id')
},
dataType:'json',
method :'POST',
success :function(response){
if(response.text){
//handle success
}else{
//handle error
}
});
更新:
$.ajax('../includes/verifycomment.php',{
data :{
'comment_body' :$(this).val(),
'userpost_post_id' : <?php echo $postid;?>,
'users_user_id' : <?php echo $userdata->user_id; ?>,
'answers_answer_id':$(this).data('id')
},
dataType:'json',
method :'POST',
success :function(response){
if(response!=""){
var _array=JSON.parse(response);
alert("Answer id is "+_array[0].answer_id);
}else{
//handle error
alert("Response is empty");
}
});
有关更多详细信息,请参见http://api.jquery.com/jquery.ajax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.