1

myfile.php

header('Content-type: application/json');

echo json_encode( array( 'error' => 'jkfshdkfj hskjdfh skld hf.' ) );

上記のコードは機能します。

しかし、それを変更すると動作しなくなります:

if( isset( $_GET['customerID'] ) ){

       // do something else error

} else {

   header('Content-type: application/json');
   echo json_encode( array( 'error' => 'jkfshdkfj hskjdfh skld hf.' ) );

}

両方の出力は正しいです:

{"error":"jkfshdkfj hskjdfh skld hf."}

しかし、私はajaxエラーが発生します:

myfile.phtml

        <?php 
            if( isset( $_GET['custID'] ) )
               echo "var custID = " . htmlentities( $_GET['custID'] ) . ";";                             
            else
               echo "var custID = null;";                             
        ?>

        $.ajax({

            url: 'php/viewCustomer.php',
            type: 'GET',
            data: {customerID: custID},
            dataType: 'json',
            cache: false,
            beforeSend: function(){

                $('#display').append('<div id="loader"> Lodaing ... </div>');

            },
            complete: function(){

                $('#loader').remove();

            },
            success: function( data ){

                if( data.error ) {

                    var errorMessage = "";

                    $.each( data, function( errorIndex, errorValue ){ 

                        errorMessage += errorValue + "\n";

                    });

                    alert( errorMessage );

                } else {

                    //$('div#customer-content table tr td').eq(0).text( '1234' );
                    //$('div#customer-content table tr td').eq(1).text( '1234' );
                    //$('div#customer-content table tr td').eq(2).text( '1234' );
                    //$('div#customer-content table tr td').eq(3).text( '1234' );
                    //$('div#customer-content table tr td').eq(4).text( '1234' );
                    alert( data );

                }                       

            },
            error: function( jqXHR ){

                alert( 'AJAX ERROR' );

            }

        });

    });
4

1 に答える 1

1

コメントから、ヌルの customerID を渡しているように聞こえますが、それが if 条件に入ることを期待していません。ただし、値が null の場合でも、$_GET['customerID']まだ設定されています。チェックをempty()代わりに変更すると、期待どおりに機能します。

if( !empty( $_GET['customerID'] ) ){
    ....
于 2013-03-07T21:13:57.033 に答える