Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP返回带有标题状态代码的自定义消息的最佳方法_Php_Jquery_Http Headers - Fatal编程技术网

PHP返回带有标题状态代码的自定义消息的最佳方法

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");

哪里是放置自定义响应消息的最佳位置。我需要使用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 */}
    )