1

我正在尝试验证 data.announce 但我收到此错误“Uncaught TypeError: Cannot read property 'announce' 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);
}
4

4 回答 4

0

您不能在 PHP 文件中返回数据,除非您将其包含在另一个 PHP 文件中的某个位置,因此您将需要它来获取您的$.post(), 通过 JSON 输出工作:

echo json_encode(array("msg" => "You'll recive an email with instructions!"));
exit(0);
于 2013-03-06T22:43:25.780 回答
0

尝试这个:

return $return = json_encode( array( "announce" => array("msg" => "You'll recive an email with instructions!") ) );
于 2013-03-06T22:45:29.833 回答
0

您没有“announce”作为要返回的数组中的键之一,只有“msg”

你要:

array("msg" => "You'll recive an email with instructions!", "announce"=>"My announcement");
于 2013-03-06T22:45:37.460 回答
-1

尝试改变:

if(!isEmpty(data.announce))

至:

if(null == data.announce)

于 2013-03-06T23:20:03.763 回答