有没有if for prepend或append .Ajax?

时间:2011-07-05 09:35:25

标签: ajax jquery append

 $.ajax({  
    type: "GET",  
    url: "../pgs/authenticate.php", 
    data: "FirstName="+ sFirstName +"&SurName="+ sSurname +"&NextOKin=" + sNOK ,  
    success: function(html){$("#Ajax_response").prepend(html);}    
        });  

我只想" prepend"如果身份验证失败,则只需:

  success: function(html){$("#Ajax_response").html(html);} 

这可能吗?

2 个答案:

答案 0 :(得分:1)

在PHP脚本中,您可以执行以下操作:

// on this part you decide based on your checks if it's true (or false)
// and what to return as content e.g. based on your authentication checks
$Success = true;
$Content = 'Some HTML';

$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);
JS中的

success: function(result)
{
   // if true
   if (result.Success)
      $("#Ajax_response").html(result.Content);
   // false (FAIL)
   else
      $("#Ajax_response").prepend(result.Content);          
}

我希望这会给你一个想法。

答案 1 :(得分:0)

if (html.match("/Failed/"))
{
$("#Ajax_response").prepend(html);
}

这会在Ajax响应html中查找单词Failed,并在找到时执行操作。显然,您知道失败响应将包含什么以及您需要查找的字符串。