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' );
}
});
});
答案 0 :(得分:1)
从评论中听起来,你传递的是一个空的customerID,并不期望它进入if条件。但即使该值为null,仍会设置$_GET['customerID']
。将支票更改为empty()
,它将按预期运作:
if( !empty( $_GET['customerID'] ) ){
....