jquery 自动完成功能适用于旧版本的浏览器,但不适用于新版本?
这是我的自动完成的 JSON 数据
{ "list" : [ {
"genericIndicatorId" : 100,
"isActive" : false,
"maxValue" : null,
"minValue" : null,
"modificationDate" : 1283904000000,
"monotone" : 1,
"name":"Abbau",
"old_name" : "abbau_change_delete_imac",
"position" : 2,
"systemGraphics" : "000000",
"unitId" : 1,
"valueType" : 1,
"description" : "Abbau",
"weight" : 1
}]}
,我编写的代码是
$("#<portlet:namespace />giName").autocomplete({
source :`enter code here` function( request, response ) {
$.post(
"<%=AJAXgetGIs%>",
{
"<%=Constants.INDICATOR_NAME%>" : request.term,
"<%=Constants.SERVICE_ID%>" : <%=serviceId%>
},
function( data ) {
response( $.map( data.list, function( item ) {
//alert(item.name + " || " + item.genericIndicatorId);
item.value = item.name;
return item;
}));
},
"json"
);
},
minLength : 2
我正在使用 jquery-ui-1.8.14.autocomplete.min.js 插件进行自动完成 我遇到的问题是它没有在新浏览器中显示所有匹配的结果。 例如,如果我输入 "an" 其中应与 "anzahl" 关键字匹配,则火虫会显示错误,例如 “a 中的错误控制字符文字字符串”。结果显示字母“as,sa...”。如有任何帮助,将不胜感激 谢谢
here is the JSON data for my auto complete
{ "list" : [ {
"genericIndicatorId" : 100,
"isActive" : false,
"maxValue" : null,
"minValue" : null,
"modificationDate" : 1283904000000,
"monotone" : 1,
"name":"Abbau",
"old_name" : "abbau_change_delete_imac",
"position" : 2,
"systemGraphics" : "000000",
"unitId" : 1,
"valueType" : 1,
"description" : "Abbau",
"weight" : 1
}]}
and the code which i wrote is
$("#<portlet:namespace />giName").autocomplete({
source :`enter code here` function( request, response ) {
$.post(
"<%=AJAXgetGIs%>",
{
"<%=Constants.INDICATOR_NAME%>" : request.term,
"<%=Constants.SERVICE_ID%>" : <%=serviceId%>
},
function( data ) {
response( $.map( data.list, function( item ) {
//alert(item.name + " || " + item.genericIndicatorId);
item.value = item.name;
return item;
}));
},
"json"
);
},
minLength : 2
i am using jquery-ui-1.8.14.autocomplete.min.js plugin for auto complete
the problem i am getting is it is not showing all the matched results in new browsers.
for example if i type "an" in which should matches to the "anzahl" keyword, the fire bug is showing error like "bad control character literal in a string". results are showing for the letters "as,sa....". any help would be appriciated
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误消息意味着您的 JSON 响应中有控制字符(例如 \n、\t 等)。根据 ECMA262 5ed,JSON 字符串中不允许使用换行符和其他控制字符。您可以通过从 PHP 或 Javascript 中转义或删除这些字符来相当轻松地修复它。
在这里您可以找到如何从 PHP 修复它的示例,因为问题很可能来自 json_encode (我假设您正在使用): http://codepad.org/Qu7uPt0E
如您所见,json_encode 不会转义 \n,因此您必须在输出之前手动执行此操作。
现在来谈谈与旧浏览器相关的谜团。如果您查看 jQuery 的 parseJSON 函数,您会注意到它首先尝试使用浏览器的内置 JSON 对象解析字符串,如果找不到任何对象,它只会执行(某种程度的)eval(即使使用换行符)。所以它可能适用于 Firefox < 3.5或IE< 8 没有本机 JSON 对象。
此外,它可能适用于其他搜索词(如 as 等),因为它们不包含具有控制字符的结果。
The error message means you have control characters in your JSON response (something like \n, \t, etc). Newlines and the other control characters are not allowed in JSON strings, according to ECMA262 5ed. You can fix it rather easily by escaping or removing those characters, either from PHP or from Javascript.
Here you can find an example of how you can fix it from PHP, as the problem most likely comes from json_encode (which I assume you're using): http://codepad.org/Qu7uPt0E
As you can see, json_encode doesn't escape the \n so you have to do it manually before outputting.
Now for the mistery related to older browsers. If you look at jQuery's parseJSON function you'll notice that it first tries to parse the string with the browser's builtin JSON object and if it doesn't find any, it will just do a (sort of) eval (which will work even with newlines). So it probably works for you on Firefox < 3.5 or IE < 8 which don't have a native JSON object.
Also, it probably works with other search terms (like as, etc) simply because they don't include a result which has control characters.
添加到 draevors 正确答案。
查看下载JSON2库
https://github.com/douglascrockford/JSON-js
这就是我解决这个问题的方法
Adding to draevors correct answer.
Look at downloading the JSON2 library
https://github.com/douglascrockford/JSON-js
That is how i got around this problem