【问题标题】:PHP best way to return custom message with header status codePHP返回带有标题状态代码的自定义消息的最佳方法
【发布时间】:2015-05-16 17:05:12
【问题描述】:

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

【问题讨论】:

    标签: php jquery http-headers


    【解决方案1】:

    虽然标题很容易访问,但更改与状态代码关联的文本并不是一个好主意。第二种方法最好。

    使用你的第二种方法,假设你已经完成了 $.getJSON。

    return $.getJSON('my-php-script.php')
        .then(
           function (data) { /* do something with data */},
           function (error) { /* do something with error */}
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多