简体   繁体   English

PHP header('Content-Type: application/json'); 不管用

[英]PHP header('Content-Type: application/json'); is not working

Sorry if it is repeated question, already tried every single suggestion in google and StackOverflow.对不起,如果它是重复的问题,已经尝试了谷歌和 StackOverflow 中的每一个建议。

For some reason, my response header always comes as Content-Type:text/html;出于某种原因,我的响应标头总是以 Content-Type:text/html; 的形式出现。 charset=UTF-8.字符集=UTF-8。

I want content type to be json (Content-Type:application/json), Not sure what am I doing wrong.我希望内容类型为 json (Content-Type:application/json),不知道我做错了什么。 here is my code这是我的代码

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

Response:回复:

在此处输入图像描述

I had the same problem.我有同样的问题。 This occurs because in one php file i had several发生这种情况是因为在一个 php 文件中我有几个

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

I've fixed it by mergin the code within the one tag only .我已经通过仅在一个标签中合并代码来修复它。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 何时在PHP中使用标题(&#39;Content-Type:application / json&#39;) - When to use header('Content-Type: application/json') in PHP `header(“Content-type:application / json”)的用法;` - The usage of `header(“Content-type:application/json”);` 如果标题中包含“ Content-Type:application / json”,则不会创建PHP 5.4.9 $ _POST超全局变量 - PHP 5.4.9 $_POST superglobal not created if 'Content-Type: application/json' is in header PHP中的内容类型标题 - Content-Type Header in PHP Slim - 如何使用“Content-Type:application / json”标题发送响应? - Slim - How to send response with “Content-Type: application/json” header? 标头Content-Type:application / json返回text / html - header Content-Type: application/json returning text/html 为什么是header(&#39;Content-Type:application / json&#39;); 在我们的网络服务中? - Why header('Content-Type: application/json'); in our web service? PHP 标头 - 内容类型:图像/jpeg - 不适用于 Internet Explorer - PHP Header - Content-type: image/jpeg - Not working for Internet Explorer 内容类型不适用于PHP - Content-type not working in PHP 未记录的PHP自动将content-type:application / json解码为$ _REQUEST - PHP undocumented automatically decode content-type:application/json to $_REQUEST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM