dsdzz04802 2017-10-05 12:55
浏览 550

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

Sorry if it is repeated question, already tried every single suggestion in google and StackOverflow.

For some reason, my response header always comes as Content-Type:text/html; charset=UTF-8.

I want content type to be json (Content-Type:application/json), Not sure what am I doing wrong. 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:

enter image description here

展开全部

  • 写回答

2条回答 默认 最新

  • drap5081683 2017-10-05 12:57
    关注

    test this way : ContentType : "application/x-www-form-urlencoded"

    评论
  • dongmeng2509 2017-10-05 12:58
    关注

    Please check if there is no warning before this line:

    header('Content-Type: application/json;charset=utf-8');
    
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部