我试图验证data.announce但我收到此错误" Uncaught TypeError:无法读取属性'宣布' of null"
所以这是我的代码
php文件:
$return = array("msg" => "You'll recive an email with instructions!");
return json_encode($return);
jquery的:
$("form[id='common-handler/register'] #submit").click(function(e) {
e.preventDefault();
if(locked == 1)
return false;
locked = 1;
var _form = $(this).closest('form').attr('id');
$.post("/"+_form, $(this).closest('form').serialize(), function(data) {
if(!isEmpty(data.announce))
$("#search_bar").html(data.msg).fadeIn("slow");
else
$("form[id='" + _form + "'] p.msg").text(data.msg);
}, "json");
});
function isEmpty(str) {
return (!str || 0 === str.length);
}
答案 0 :(得分:0)
你不能在PHP文件中返回数据,除非你把它包含在另一个PHP文件中的某个地方,所以你需要这个来获取你的$.post()
,通过JSON输出工作:
echo json_encode(array("msg" => "You'll recive an email with instructions!"));
exit(0);
答案 1 :(得分:0)
试试这个:
return $return = json_encode( array( "announce" => array("msg" => "You'll recive an email with instructions!") ) );
答案 2 :(得分:0)
你没有将“announce”作为你要返回的数组中的一个键,只有“msg”
你想:
array("msg" => "You'll recive an email with instructions!", "announce"=>"My announcement");
答案 3 :(得分:-1)
尝试更改:
如果(!的isEmpty(data.announce))
为:
if(null == data.announce)