PHP返回带有标题状态代码的自定义消息的最佳方法
哪里是放置自定义响应消息的最佳位置。我需要使用jquery访问响应消息,并向用户发出警报PHP返回带有标题状态代码的自定义消息的最佳方法,php,jquery,http-headers,Php,Jquery,Http Headers,哪里是放置自定义响应消息的最佳位置。我需要使用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");
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 */}
)