2011-07-05 8 views
0
$.ajax({ 
    type: "GET", 
    url: "../pgs/authenticate.php", 
    data: "FirstName="+ sFirstName +"&SurName="+ sSurname +"&NextOKin=" + sNOK , 
    success: function(html){$("#Ajax_response").prepend(html);}  
     }); 
認証がちょうど他の障害が発生した場合、私は唯一の「先頭に追加」したい

.Ajaxの前に追加するか、または追加するのか?

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

これは可能ですか?

答えて

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

これは、AjaxレスポンスhtmlでFailedという単語を探し、見つかった場合にアクションを実行します。明らかに、失敗の応答に含まれるものと探している文字列が分かります。 PHPスクリプトで

+0

AjaxレスポンスがHTMLではなくPHPである場合はどうなりますか?これは(html.match( "/ Failed /"))はまだ適用されますか? – SirBT

+0

はい。あなたが探しているものに失敗したという言葉を変える限り、どんな文字列でも動作します。 –

1

のように、あなたが何かを行うことができます:私はそれはあなたのアイデアを与える願っています

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

:JSで

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

+0

ここで$ Contentは 'Some HTML'に割り当てられていますが、コンテンツがPHPの場合はどうなりますか? "hello world"; ?あなたの返信である – SirBT

+0

@SirBTを楽しみにして、hello worldを '$ Content'に入れましょう。 '$ Content = 'Hello World';'。もちろん、 '$ Success'変数が真であるか偽であるかをチェックする必要があります。 – tradyblix

+0

@ Tradblix ...ありがとう。 $( "#Ajax_response")、html(result.Content)または$( "#Ajax_response")を得ることはできません。prepend(result.Content);働くしかし、$( "#Ajax_response")。prepend(result);うまく動作します。私は何が欠けていますか? – SirBT

関連する問題