duanhongxian6982 2016-12-06 10:03
浏览 72
已采纳

包含html标记时,JSON字段返回null

I am trying to expose some data in a table that contains HTML tags, when I access the page all of the fields not containing HTML are fine but any containing html return null.

I have tried setting the fields to VarChar and Text but neither seem to work. The table is also set to utf8_general_ci.

request_step_content.php

<?php
header("Content-Type:application/json");
header("Access-Control-Allow-Origin: *");

$user = "userName";
$pass = "userPassword";
$table = "myTable";

$db=new PDO("mysql:host=localhost;dbname=$table", $user, $pass);

$query = "SELECT * FROM steps";

try {
    $stmt = $db->query($query);
    }
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error.";
    die(json_encode($response));
}

$rows = $stmt->fetchAll();

if($rows) {
    $response["success"] = 1;
    $response["message"] = "Step";
    $response["step"] = array();

    foreach ($rows as $row) {
        $post = array();
        $post["id"] = $row["intID"];
        $post["heading"] = $row["strStepheading"];
        $post["keyPrinciple"] = $row["strKeyPrinciple"];
        $post["pillar"] = $row["strPillar"];
        $post["introduction"] = $row["memIntroduction"];
        $post["actionSteps"] = $row["memActionSteps"];
        $post["actionPoints"] = $row["memActionPoints"];
        $post["studyAndUnderstanding"] = $row["memStudyUnderstanding"];

        array_push($response["step"], $post);
    }
    echo json_encode($response);
}
else {
    $response["success"] = 0;
    $response["message"] = "No Steps Available";
    die(json_encode($response));
}
?>

json response

{"success":1,
 "message":
           "Step",
           "step":[{"id":"1",
                          "heading":"Test Heading",
                          "keyPrinciple":"Key Principle: Test Key Principle",
                          "pillar":"Pillar 1: Pillar",
                          "introduction":null,
                          "actionSteps":null,
                          "actionPoints":null,
                          "studyAndUnderstanding":null}]}
  • 写回答

2条回答 默认 最新

  • douwei9759 2016-12-06 11:05
    关注

    I fixed the issue with some help of @Fky's answer regarding encoding.

    Using utf_encode() I now have all fields including the html content in my JSON fields. Here's the edited code:

            foreach ($rows as $row) {
            $post = array();
            $post["id"] = $row["intID"];
            $post["heading"] = $row["strStepheading"];
            $post["keyPrinciple"] = $row["strKeyPrinciple"];
            $post["pillar"] = $row["strPillar"];
            $post["introduction"] = utf8_encode($row["memIntroduction"]);
            $post["actionSteps"] = utf8_encode($row["memActionSteps"]);
            $post["actionPoints"] = utf8_encode($row["memActionPoints"]);
            $post["studyAndUnderstanding"] = utf8_encode($row["memStudyUnderstanding"]);
    

    Thank you for the help all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码