我的文件.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错误:
我的文件.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' );
}
});
});