Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP JSON不起作用,有人能告诉我为什么吗?_Php_Jquery_Json - Fatal编程技术网

PHP JSON不起作用,有人能告诉我为什么吗?

PHP JSON不起作用,有人能告诉我为什么吗?,php,jquery,json,Php,Jquery,Json,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

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

            }

        });

    });

$.ajax({
url:'php/viewCustomer.php',
键入:“GET”,
数据:{customerID:custID},
数据类型:“json”,
cache:false,
beforeSend:function(){
$('#display')。追加('Lodaing…');
},
完成:函数(){
$(“#加载程序”).remove();
},
成功:功能(数据){
if(data.error){
var errorMessage=“”;
$.each(数据、函数(errorIndex、errorValue){
errorMessage+=errorValue+“\n”;
});
警报(错误消息);
}否则{
//$('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')。等式(2)。文本('1234');
//$('div#customer content table tr td')。等式(3)。文本('1234');
//$('div#customer content table tr td')。等式(4)。文本('1234');
警报(数据);
}                       
},
错误:函数(jqXHR){
警报('AJAX错误');
}
});
});

从注释中可以看出,您正在传递一个空customerID,并且不希望它进入if条件。但是,即使该值为null,
$\u GET['customerID']
仍然被设置。将检查改为
empty()
,它将按预期工作:

if( !empty( $_GET['customerID'] ) ){
    ....

从注释中可以看出,您正在传递一个空customerID,并且不希望它进入if条件。但是,即使该值为null,
$\u GET['customerID']
仍然被设置。将检查改为
empty()
,它将按预期工作:

if( !empty( $_GET['customerID'] ) ){
    ....

你有什么错误?设置
customerID
时会发生什么?错误是来自以下错误的“AJAX错误”:函数(jqXHR){alert('AJAX error');}您正在通过AJAX请求传递一个customerID,因此它将进入if-您希望得到什么输出?数据:{customerID:custID}=custID来自何处?Chad,如果设置了customerID,我现在什么也不做。如果块为空,会出现什么错误?设置
customerID
时会发生什么?错误是来自以下错误的“AJAX错误”:函数(jqXHR){alert('AJAX error');}您正在通过AJAX请求传递一个customerID,因此它将进入if-您希望得到什么输出?数据:{customerID:custID}=custID来自何处?Chad,如果设置了customerID,我现在什么也不做。如果块为空。