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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?