JSON字符串中的未终止字符串文本,其中包含来自SQLServer的带有HTML标记的值
Unterminated string literal in JSON string with values from SQLServer with HTML tags
我一直在试图解决一个'未终止字符串文字错误',我得到在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+"¬e="+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+$/, "");
}
相关文章:
- 如何使用JQuery在Javascript中转换字符串中的HTML元素
- 如何使用JQuery在HTML中创建包含字符串参数的引号的onclickjavascript链接
- 如何使用gump任务将html转换为javascript字符串
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- HTML字符串作为上下文
- 字符串中的
标记未正确在 HTML 中创建新行 - 将HTML(支持文件)中的令牌(字符串、数字等)传递给LogiXML
- 如何使用javascript选择字符串的部分,添加html标记或删除部分
- Visual Studio 2015 扩展将 JavaScript 文件中的字符串 HTML 视为 HTML 编辑
- 如何使用jquery插入多行字符串html
- 创建一个通过页面的查询字符串HTML表单
- PayPal确认,然后返回一个字符串HTML(js或任何其他..)
- 使用Javascript DOMParser将单字符串HTML格式化为多行选项卡HTML
- 如何转义字符串(HTML)中的多个层次引号
- 解析笑脸与文本字符串html和保持笑脸在显示顺序
- Regex帮助,JS尝试将字符串(html)转换为数字
- removeAttr在字符串html
- jqgrid asp.net-Javascript字符串HTML合成
- 如何用另一个标记替换字符串HTML标记
- 将变量追加到字符串html中