我正在使用PHP json_encode()来返回一些由jQuery检索的数据ajax():
简化的JS:
$.ajax({
dataType: 'json',
contentType: 'application/json',
cache: false,
type: 'POST',
url: './api/_imgdown.php',
error: function(jqXHR, textStatus, errorThrow) {
console.log(jqXHR, textStatus, errorThrow);
},
success: function(data, textStatus, jqXHR) {
console.log(data, textStatus, jqXHR);
}
});
Run Code Online (Sandbox Code Playgroud)
PHP是:
header('Content-Type: application/json; charset=UTF-8');
//default apiResponse
$apiResponse = [
"status" => 1,
"message" => "success",
"data" => null
];
Run Code Online (Sandbox Code Playgroud)
然后当php运行我的代码时,它会通过添加这些数据结束:
$apiResponse['data'][] = [
"mid" => (int)$mid,
"card_type" => $card_type,
"header_size" => (int)$headers['Content-Length'],
"saved_size" => (int)filesize($imgSavePath),
"saved_path" => $imgSavePath
];
//spit out the JSON
echo …Run Code Online (Sandbox Code Playgroud)