Javascript EasyHP 5.3.5.0和Ajax:可能是json_编码
我在pc上使用EasyHP 5.3.5.0构建一些Ajax简单的动态操作 我想在单击“添加项目”按钮时刷新列表,以便该按钮在单击时有一个事件 第一个javascript页面在click事件中执行此操作:Javascript EasyHP 5.3.5.0和Ajax:可能是json_编码,javascript,php,ajax,json,easyphp,Javascript,Php,Ajax,Json,Easyphp,我在pc上使用EasyHP 5.3.5.0构建一些Ajax简单的动态操作 我想在单击“添加项目”按钮时刷新列表,以便该按钮在单击时有一个事件 第一个javascript页面在click事件中执行此操作: $.post("operation/insert.php", { type : "cliente", name : nome, surname : cognome , description : descrizione, day : giornoNascita, month : meseNas
$.post("operation/insert.php", { type : "cliente", name : nome, surname : cognome , description : descrizione, day : giornoNascita, month : meseNascita, year : annoNascita }, function(data){
alert("HELLO!");
},'json');
在Php页面“operation/insert.Php”中,在对变量进行初步检查后,我运行以下代码:
$q_add_client = mysql_query($query);
$return = array(
id => mysql_insert_id(),
response => 0
);
echo json_encode($return);
我确信查询已执行,因为我可以在数据库中看到新项。但是,最后,我没有看到我的警报(“HELLO!”),因此我认为json_encode函数及其参数存在问题。它不会在Javascript模块的第$.post(…)行执行回调函数
有人能帮我吗?谢谢使用jQuery.ajax方法尝试这种语法
$.ajax({
url:'operation/insert.php',
type:'POST',
data:{
type : "cliente", name : nome, surname : cognome , description : descrizione, day : giornoNascita, month : meseNascita, year : annoNascita
},
success:function(data){
alert('HELLO');
}
})
如果你有一个错误,比如你下面的评论
event.returnValue is deprecated. Please use the standard event.preventDefault() instead
在您的页面中编写此代码
if (!event.preventDefault) {
event.preventDefault = function() {
event.returnValue = false; //ie
};
}
如果您告诉$.post希望从服务器获得“json”,并且 无论何时向输出发送json,都应该设置正确的内容类型 第一:
我已经在php文件的顶部有了代码头(“内容类型:application/json”);这很奇怪,它在我的其他一些项目中运行!如果您直接访问:operation/insert.php,您得到了正确的输出吗?我注意到$return数组中缺少引号(修复了答案中的引号),所以可能是一些语法错误造成的问题?太好了!这就是问题所在,但我不知道为什么它在我的其他应用程序中运行,没有引号。。。大谜团谢谢!在浏览器上打开控制台,告诉我结果是什么我的控制台显示:event.returnValue已弃用。请改用标准的event.preventDefault()。我不知道为什么会出现此警告,但我正在使用一些带有event onclick和onblur项“input”和“textarea”的函数。我说的是这些函数:函数clickInput(nameim,text){var val=$(“input[name=”+nameim+“]”]).val();if(val==text)$(“input[name=”+nameim+“]”)).val(“”;}函数onblurInput(nameItem,text){var val=$([input[name='”+nameItem+“]]).val();if(val==”)$([input[name='”+nameItem+“]])val(text);}问题是json响应的属性中有引号,但我不知道为什么它在其他应用程序中以其他方式运行,没有引号。谢谢你的支持
$q_add_client = mysql_query($query);
$return = array(
'id' => mysql_insert_id(),
'response' => 0
);
header("content-type: application/json");
echo json_encode($return);