【问题标题】:PHP header('Content-Type: application/json'); is not workingPHP header('Content-Type: application/json');不管用
【发布时间】:2018-03-17 14:30:16
【问题描述】:

对不起,如果它是重复的问题,已经尝试了谷歌和 StackOverflow 中的每一个建议。

由于某种原因,我的响应标头始终为 Content-Type:text/html;字符集=UTF-8。

我希望内容类型为 json (Content-Type:application/json),不知道我做错了什么。这是我的代码

    <?php
    header('Access-Control-Allow-Origin: *');

    define('__ROOT__', dirname(dirname(__FILE__)));
    require_once(__ROOT__.'/api/csvtojson.php');
    require_once(__ROOT__.'/api/retrieve-login-data.php');
    require_once(__ROOT__.'/api/access-control.php');
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    //Make sure that it is a POST request.
    if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
        throw new Exception('Request method must be POST!');
    }

    //Make sure that the content type of the POST request has been set to application/json
    $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
    if(strcasecmp($contentType, 'application/json') != 0){
        throw new Exception('Content type must be: application/json');
    }

    //Receive the RAW post data.
    $content = trim(file_get_contents("php://input"));

    //Attempt to decode the incoming RAW post data from JSON.
    $decoded = json_decode($content, true);

    //If json_decode failed, the JSON is invalid.
    if(!is_array($decoded)){
        throw new Exception('Received content contained invalid JSON!');
    }
$filename =     $decoded['filename'];

   // open csv file
    if (!($fp = fopen($filename, 'r'))) {
      die("Can't open file...");
      }
    //read csv headers
    $key = fgetcsv($fp,"1024",",");
    // parse csv rows into array
    $json = array();
    while ($row = fgetcsv($fp,"1024",",")) {
      $json[] = array_combine($key, $row);
      }
    // release file handle
    fclose($fp);
    // encode array to json
    header('Content-Type: application/json;charset=utf-8');
    echo (json_encode($json, JSON_PRETTY_PRINT));

回复:

【问题讨论】:

  • 你能试着做一个更小的例子,只发出一些静态 JSON 吗?
  • 已经尝试过以下代码,我得到相同的 contentType name = "John"; $myObj->年龄=30; $myObj->city = "纽约"; $myJSON = json_encode($myObj);回声 $myJSON; ?>
  • 对于任何长代码块,只需编辑您的问题。请注意,在该位中没有设置标头。
  • 在短代码中设置标题后您的权利,但在我的长代码中仍然无法正常工作
  • 您可能需要禁用大量代码才能找到问题。如果它发出 HTML,那么你可能只是得到了一个错误。

标签: php json header content-type


【解决方案1】:

以这种方式测试: ContentType : "application/x-www-form-urlencoded"

【讨论】:

    【解决方案2】:

    请检查此行之前是否有警告:

    header('Content-Type: application/json;charset=utf-8');
    

    【讨论】:

    • 没有警告并且回显正在打印以下结果 [ { "ID": "A:
    • 请问前面有没有空格
    • 前面没有空格
    • @Yama ,看来您所做的一切都很好。您可以尝试的最后一件事是从 json_encode 中删除 JSON_PRETTY_PRINT。我知道这不是一个合适的解决方案,但请试一试,
    • 已经尝试移除 json_pretty_print,并且已经尝试将 echo 替换为 print 和 print_r
    【解决方案3】:

    我遇到了同样的问题。这是因为在一个 php 文件中我有几个

    部分。
    ======== File.php =========
    <?php
    //some code
    ?>
    
    
    <?php
    //some other code
    ?>
    
    ===========================
    

    我已经通过仅在一个标签中合并代码来修复它。

    【讨论】:

      猜你喜欢
      • 2017-11-06
      • 2013-04-07
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2020-11-14
      相关资源
      最近更新 更多