Ajax 为什么else语句在这里不起作用?

Ajax 为什么else语句在这里不起作用?,ajax,Ajax,以下代码无法在else语句下获得正确响应: function ajax_response(){ if(is_user_logged_in() && check_key() ){ $result = $do_something; if($result){ ajax_response_string(1,'success!'); }else{ ajax_response_string(-1,'there is a pro

以下代码无法在else语句下获得正确响应:

function ajax_response(){
 if(is_user_logged_in() && check_key() ){
    $result = $do_something;
     if($result){
        ajax_response_string(1,'success!');
      }else{
         ajax_response_string(-1,'there is a problem, please try again!');
      }
 }else{
   ajax_response_string(-1,'you must login first.');
}
}


当我注销并尝试单击触发此ajax函数的链接时,我得到了“undefined”的响应,应该是“you must login first”。哪里出错了?

$result=//行出现语法错误,因为此行未终止。试试这个:

function ajax_response()
{
     if(is_user_logged_in() && check_key() )
     {
        $result =  doSomething; //error was here as there was no line terminator (;) present.

         if($result)
         {
            ajax_response_string(1,'success!');

         }
          else  
          {
             ajax_response_string(-1,'there is a problem, please try again!');
          }
     }
     else
        {
           ajax_response_string(-1,'you must login first.');
        }

}

我刚刚发现,这不是else语句,而是ajax响应url不适用于已注销的用户。所以,我的问题不再有效。如有必要,请把它关上

首先,您有一个语法错误!!在最后一个else之后打开的braket没有关闭,或者在
ajax\u response\u string(-1,“您必须先登录”)之后关闭它或只是删除它。对不起,我忘了关闭这篇文章中的最后一个,我只是添加了它。但是在我的原始代码中,它是关闭的,并且没有得到正确的响应。打印
$result
以验证它既不是
null
也不是
未定义的
。如果注销,则模具代码不起作用。似乎代码在注销时未被触发,但如果未触发,为什么仍然可以收到“未定义”消息?发布此问题时出错,我编辑了我的问题。@Jenny,第三行仍然显示
$result=//做点什么$do something