放置自定义响应消息的最佳位置在哪里。我需要使用 jquery 访问响应消息并将其提醒用户
function return_status($code, $message) {
//This way
header("HTTP/1.1 $code $message");
die();
//OR This way
header('Content-Type: application/json');
header("HTTP/1.1 $code");
die(json_encode(['code' => $code, 'message' => $message]));
}
虽然标头易于访问,但更改与状态代码关联的文本不是一个好主意。 您的第二种方法是最好的。
使用第二种方法,假设您已经完成了$.getJSON。
return $.getJSON('my-php-script.php')
.then(
function (data) { /* do something with data */},
function (error) { /* do something with error */}
)