1

私のphpページには、タグの外側で使用されるHTMLがある<?phpため、このjQueryコードを使用すると;

    $.ajax(
    {
        type : "post",
        dataType: "html",
        url : "url.php",
        data : "to_uid="+to_uid+"&from_uid="+from_uid,
        cache: false,
        success : function(response)
        {
            alert(response);
        }
    });

エコー(エラーまたは成功メッセージのいずれか)を警告しますが、そのphpページで使用したすべてのHTMLをエコーし​​ます。

そのエラー/成功アラートのみをエコーし​​、他の HTML をエコーし​​ない回避策はありますか? dataTypeをからhtmlに変更しようとしましjsonたが、エラー/成功のエコーはまったく警告されません。

助けてください。

4

2 に答える 2

2

あなたのurl.phpセットで配列

$return = array();
if ( SOME CONDITION ) {
   $return['html'] = "<html> ... </html>";
   $return['message'] = 'Success Message';
   $return['stat'] = 'Success';
} else {
   $return['message'] = 'Fail Message';
   $return['stat'] = 'Failed';
}
echo json_encode($return);

そしてあなたのajaxで

$.ajax(
{
    type : "post",
    dataType: "json",
    url : "url.php",
    data : "to_uid="+to_uid+"&from_uid="+from_uid,
    cache: false,
    success : function(response)
    {
        alert(response.message);
        if (response.stat == 'success') {
           do_some_thing(response.html);
        }

    }
});
于 2013-10-30T17:46:18.253 に答える