JSON字符串中的未终止字符串文本,其中包含来自SQLServer的带有HTML标记的值

Unterminated string literal in JSON string with values from SQLServer with HTML tags

本文关键字:字符串 HTML SQLServer 包含 终止 文本 JSON      更新时间:2023-09-26

我一直在试图解决一个'未终止字符串文字错误',我得到在Firebug时,我从下拉菜单中选择'乳房' - http://wftp.whclpreview.com/search.html并点击提交按钮。错误指向我用粗体突出显示的引号-

SpecialGroups":"一"、"NationalAudits":"na"、"SpecialInterests": "

基本上这个错误是$操作的结果。ASP. ajax调用。. NET函数,以JSON格式从SQLServer数据库(包含一个名为SpecialInterests的字段)返回数据。数据库附加到一个管理员,允许用户通过CKEDITOR插件格式化文本。

如果文本是简单的,那么一切都是好的,但如果它包含任何HTML标签或换行符,那么我得到上述错误。

如果您正在使用AJAX,您可以传递对象文字或使用de数据构建查询字符串:例如

 //if the variable contains HTML string,
 //ensure to encode it before sending the request
 var note = "  <strong>Important:</strong><br/> Additional notes. ";
 note = encodeHtml(note);
 //Object Literal
 var params = { "date": date, "expiry": expiry, "priority": priority, "note": note };
 //Query string to append to the url
 var paramsUrl = buildUrlParams(params).join("&");
  $.ajax({
    type: "POST",
    url:  "myservice/client",
    //ugly way
    //data: "date="+date+"&expiry="+expiry+"&priority="+priority+"&note="+note,
    //passing data as Literal Object        
    data: params,
    //passing data as querystring
    //data: paramsUrl,
    success: function(data){ alert(data); }
  });

  //Creates an array of parameters to build an URL querystring
  //@obj: Object to build the array of parameters
  function buildUrlParams(obj) {
      return $.map(obj, function(value, key) {
          return key + "=" + value;
      });
  }

  //Encodes the HMTL to their respective HTML entities
  //@text: the HTML string to encode
  function encodeHtml(text) {
    var div = document.createElement("div");
    if ("innerText" in div) div.innerText = text;
    else div.textContent = text;
    return div.innerHTML.replace(/^'s+|'s+$/, "");
  }