表绑定以及何时使用htmlspecialchars - JSON.parse错误

时间:2016-01-31 12:50:17

标签: javascript php jquery json

我正在使用无脂肪框架。在控制器中,我返回json数据。

我正在使用htmlspecialchars - 我需要显示html和css。

$results = array();
$counter = 0;
foreach ($rows as $row) {
  $results[$counter]['id'] = $row['id'];
  $results[$counter]['number'] = $row['number'];
  $results[$counter]['text'] = htmlspecialchars($row['text']);
  $counter++;
}
echo json_encode($results);

在我看来,我有:

function onCommand(sender, command, fArgs) {

  obj = FunctionArgsToJsonObject(fArgs);

  if (sender == "tbl_data" ) {
    switch (command) {
      case "edit_text": {

        CallAPI("GET", "text/" + obj.id, function (data) {
          data = JSON.parse(data);

          BindForm(data, "info");

          $('#myModal').modal('show');
        });
        break;
      }
    }
  }
}
    <table id="tbl_data" class="table table-striped table-bordered table-hover">
        <thead>
        <tr>
            <th binding="id" action="">ID</th>
            <th binding="number" action="">№</th>
            <th binding="text" action="">Text</th>
            <th command="edit_text" buttonStyle="warning" buttontitle="edit"  icon="edit" >Edit</th>
        </tr>
        </thead>
    </table>


<!--Modal for edit -->
<div class="row col-md-12" style="width:1000px !important;">
    <div class="modal fade" id="myModal"  role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="vertical-alignment-helper">
            <div class="modal-dialog vertical-align-center">
                <div class="modal-content">

                    <div class="modal-body">
                        <form class="form-horizontal" role="form" id="info">
                            <div class="form-group">
                                <label class="col-sm-2" for="form-field-1">№ </label>
                                <div class="col-sm-9">
                                    <input id="frm_number" class="col-xs-10 col-sm-5" type="text">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2" for="form-field-1">Text</label>
                                <div class="col-sm-9">
                                    <textarea id="frm_text" class="col-xs-10 col-sm-5" cols="100" rows="5"></textarea>
                                </div>
                            </div>
                        </form>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

例如,我在列文本中的文字是:

<a href='register.php' style='font-size:16px;'>Register</a>

有错误:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 77 of the JSON data

如何解决这个问题?

提前谢谢!

已编辑:我试过了:

$results[$counter]['text'] = addslashes(htmlspecialchars($row['text']));

并且它正在工作但仅适用于单引号。

如何逃避双引号?

1 个答案:

答案 0 :(得分:0)

我找到了问题的解决方案,它是:

在控制器I中使用以下内容:

$results[$counter]['text'] = htmlentities($row['text'],ENT_QUOTES);

我在js中查看了它:

CallAPI("GET", "text/", function (data) {
    data = data.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    BindTable($("#tbl_data"), data, "", null, { hasSearchPaging: true,  hasSorting: true });
});