开发者

Is there a If for to prepend or append in .Ajax?

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

I only want to "prepend" IF the authentication fails else just:

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

Is this possibl开发者_如何学Ce?


In your PHP script, you can do something like:

// 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);

in JS:

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

I hope that gives you an idea.


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

This looks for the word Failed in the Ajax response html and does the action if found. Obviously you know what the failure response will contain and what string you need to be looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜