簡體   English   中英

表綁定以及何時使用htmlspecialchars-JSON.parse錯誤

[英]Table binding and when when using htmlspecialchars - JSON.parse error

我正在使用無脂肪框架。 在控制器中,我返回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']));

並且有效;但僅適用於單引號。

如何也轉義雙引號?

我找到了解決問題的方法,它是:

在控制器中,我使用以下內容:

$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 });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM