[英]jQuery $.post Callback Function (data) is Undefined
I submit a form using jQuery to a php file on my server. 我使用jQuery向服务器上的php文件提交表单。 Everything works... (the php file gets the right post variables, makes a database entry etc.) But on the response, sometimes 'data' goes wacky.
一切正常...(php文件获取正确的post变量,创建数据库条目等。)但是在响应时,有时“数据”变得古怪。
$('#form_submit').click( function() {
$.post("path/to/script.php", $('#form').serialize(), function(data) {
if ( data.status == 1 ) {
alert('awesome sauce');
} else {
alert('crap');
}
}, "json");
});
php script returns (on success) php脚本返回(成功)
$response['status'] = 1;
$response['message'] = 'worked';
echo json_encode($response);
exit();
I'm getting a whole lot of crap, and not enough awesome sauce. 我得到很多废话,而且酱汁不够好。
Does anyone have an idea why sometimes 'data.status' is undefined, and sometimes it isn't? 有谁知道为什么有时未定义“ data.status”,有时未定义?
Try it like this> 像这样尝试>
$('#form_submit').click( function() {
$.post("path/to/script.php", $('#form').serialize(), function(data) {
var obj = jQuery.parseJSON(data);
if ( obj.status == 1 ) {
alert('awesome sauce');
} else {
alert('crap');
}
});
});
How does exit() behave with regards to output buffering? exit()在输出缓冲方面表现如何? Does it flush the output buffer?
是否刷新输出缓冲区?
try this one: 试试这个:
$('#form_submit').click( function() {
$.post("path/to/script.php", $('#form').serialize())
.success(function(){
alert('awesome sauce');
}).error(function(){
alert('crap');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.