![](/img/trans.png)
[英]AJAX call return success message but post data never insert into db
[英]ajax call with a return json never catch in success
我正试图从我的ajax调用中获取一个变量:
$.ajax({
type: "POST",
url: "insert",
data: { title:title, start:dstart, end:dend },
contentType: "application/json",
dataType : 'json',
success : function(data) {
data = JSON.parse(data);
console.log('data = '); // is showing the data with double quotes
console.log(data);
}
});
还有我的PHP:
$id = $calendar->getId();
$json = array ( 'id' => $id );
var_dump(json_encode($json));
json_encode($json);
用我的var_dump
我可以看到我的json_encore
,例如:
string '{"id":156}' (length=10)
但是在我的ajax()
成功中, console.log()
在我的控制台中没有显示任何内容。
我在哪里可以看到我的success: function(data)
是否为空? 我会在我的ajax成功中抓住id。
更新:问题已修复。 事实上我正在使用symfony,我没有在我的动作插入中看到我的PHP,symfony(indexSuccess.php)调用的页面不是空的,这就是为什么它根本不工作。)
如果你看看你的PHP代码,你基本上什么都不做json_encode()的输出......
请将PHP代码的最后一行更新为:
echo json_encode($json);
现在您应该获得所需的数据作为响应。
编辑:@ 1nsan3,您在评论中询问echo是否与var_dump()相同...我想你在这里得到答案: PHP中的echo,print和print_r有什么区别?
EDIT2:
请删除JSON.parse()调用。 当使用dataType : 'json'
,jQuery已经解析了您的AJAX请求的响应,如http://api.jquery.com/jQuery.ajax中所述
你确定你的php返回有效的json数据吗?
PHP
$id = $calendar->getId();
$json = array('id' => $id);
echo json_encode($json); <-- your php code must echo only this valid json,
return; make sure no other values are echoed
above or below this which will break json.
JS
success : function(data) {
if($.trim(data) != '') {
console.log(data);
} else {
console.log('no data');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.