Jquery typeahead.js预取问题

Jquery typeahead.js预取问题,jquery,typeahead,prefetch,typeahead.js,Jquery,Typeahead,Prefetch,Typeahead.js,我在谷歌应用程序引擎项目中使用typeahead.js,在使用预取时遇到问题 当我使用local时,typeahead可以正常工作,但如果我将同一数据集复制到json文件并使用预取,typeahead就不工作,即不显示任何建议 以下是我使用本地语言编写的代码版本: <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Demo</title>

我在谷歌应用程序引擎项目中使用typeahead.js,在使用预取时遇到问题

当我使用local时,typeahead可以正常工作,但如果我将同一数据集复制到json文件并使用预取,typeahead就不工作,即不显示任何建议

以下是我使用本地语言编写的代码版本:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <div><input type="text" name="typeahead-example" placeholder="Type here" class="typeahead-example"></div>
    <script src="./js/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="./js/hogan.js" type="text/javascript"></script>
    <script src="./js/typeahead.min.js" type="text/javascript"></script>
    <script>
        $(document).ready(function(){
                $('input.typeahead-example').typeahead({
                  name: 'example',
                  local: [{value: 'Abc Def', tokens: ['Abc', 'Def'], name: 'random1', val2: 'A', val3: 'B'},
                    {value: 'Def Ghi', tokens: ['Def', 'Ghi'], name: 'random2', val2: 'C', val3: 'D'},
                    {value: 'Ghi Jkl', tokens: ['Ghi', 'Jkl'], name: 'random3', val2: 'E', val3: 'F'},
                    {value: 'Jkl Mno', tokens: ['Jkl', 'Mno'], name: 'random4', val2: 'G', val3: 'H'}],
                  limit: 3,
                  valueKey: 'name',
                  template: '<p>{{value}}</p>',
                  engine: Hogan
                });
            });
    </script>
</body>
</html>
这个问题发生在Chrome和Firefox中。在chrome中调试时,我可以看到example.json文件被提供和接收。network activity example.json的预览和响应显示了文件的全部内容。但是,在chrome调试器的resources部分,本地存储是空的。没有控制台错误,只有此消息

XHR finished loading: "http://localhost:8000/json/example.json". jquery-1.10.2.js:8706
send jquery-1.10.2.js:8706
jQuery.extend.ajax jquery-1.10.2.js:8136
jQuery.(anonymous function) jquery-1.10.2.js:8282
jQuery.extend.getJSON jquery-1.10.2.js:8265
c.mixin._loadPrefetchData typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin.initialize typeahead.min.js:7
proxy jquery-1.10.2.js:827
(anonymous function) typeahead.min.js:7
jQuery.extend.map jquery-1.10.2.js:782
g typeahead.min.js:7
jQuery.extend.each jquery-1.10.2.js:657
jQuery.fn.jQuery.each jquery-1.10.2.js:266
b.initialize typeahead.min.js:7
jQuery.fn.typeahead typeahead.min.js:7
(anonymous function) prefetch.html:14
fire jquery-1.10.2.js:3048
self.fireWith jquery-1.10.2.js:3160
jQuery.extend.ready jquery-1.10.2.js:433
completed
关于,我没有跨域问题,关于,我已将ttl设置为0,但问题仍然存在

任何帮助都将不胜感激

  • 更新* 为了回应@NitzanShaked的建议,我添加了一些控制台日志
首先,我尝试了这个,但在控制台中没有注册任何内容

$(document).ready(function(){
    $('input.typeahead-example').typeahead({
      name: 'example',
      prefetch: {
        url: './json/example.json', 
        ttl: '0',
        filter: function (data) {
            console.log(data);
            for (var i = 0; i < data.length; i++) {
                datum = data[i];
                console.log(datum);
            }
            return data;
        }
      },
      limit: 3,
      valueKey: 'name',
      template: '<p>{{value}}</p>',
      engine: Hogan,
    });
});

您的问题出现在
example.json
中,事实证明。。。您需要使用双引号引用键名,并且所有字符串(例如,
标记中的
)也需要用双引号引用

例如,这对我很有用:

[{"value": "Abc Def", "tokens": ["Abc", "Def"], "name": "random1", "val2": "A", "val3": "B"}]

url
/json/example.json
更改为
/json/example.json
(删除前导点)也是明智的做法。

@diddleboo
local
识别不带引号的数据集,因为此时您正在使用Javascript对象文本,这与JSON不同

Javascript Object Literal只是Javascript环境中的一个
对象
,而语言规范表示命名对象属性不需要双引号


JSON代表JavaScript对象表示法。这是一种数据传输格式,它的语法要求属性名使用双引号。在此处查看有关JSON定义的详细信息

请尝试不为预取数据集指定
名称
(在您的示例中为
“示例”
),以确保它不是从本地存储中引入的。@Nitzanshake感谢您的建议。尝试了该操作,但没有产生任何差异添加
过滤器
,然后使用
控制台.log
打印参数。确保它有意义。如果是,请尝试从
过滤器返回一个虚拟数据,看看它是否有效。@再次感谢您-根据建议更新了上述信息
过滤器
应该是
预回迁
的一部分。再试一次?成功了-谢谢!奇怪的是,
local
识别出了没有引号的数据集。我很高兴。如果答案解决了您的问题,请随意投票并接受答案。已接受-仍然是堆栈交换noob,因此需要更多的代表投票,但一旦我得到答案,我会更清楚地了解问题的根本原因。
$(document).ready(function(){
    $('input.typeahead-example').typeahead({
      name: 'example',
      prefetch: {
        url: './json/example.json', 
        ttl: '0',
        filter: function (data) {
            console.log(data);
            for (var i = 0; i < data.length; i++) {
                datum = data[i];
                console.log(datum);
            }
            return data;
        }
      },
      limit: 3,
      valueKey: 'name',
      template: '<p>{{value}}</p>',
      engine: Hogan,
    });
});
$(document).ready(function(){
    $('input.typeahead-example').typeahead({
      name: 'example',
      prefetch: {url: './json/example.json', ttl: '0'},
      limit: 3,
      valueKey: 'name',
      template: '<p>{{value}}</p>',
      engine: Hogan,
    }).bind('typeahead:opened', function (obj, datum) {
                console.log(obj);
                console.log(datum);
                console.log(datum.val2);
                });
});
jQuery.Event {type: "typeahead:opened", timeStamp: 1378502920480, jQuery1102039872112357988954: true, isTrigger: 3, namespace: ""…}
currentTarget: input.typeahead-example tt-query
data: null
delegateTarget: input.typeahead-example tt-query
handleObj: Object
isTrigger: 3
jQuery1102039872112357988954: true
namespace: ""
namespace_re: null
result: undefined
target: input.typeahead-example tt-query
timeStamp: 1378502920480
type: "typeahead:opened"
__proto__: Object
 prefetch.html:22
undefined prefetch.html:23
Uncaught TypeError: Cannot read property 'val2' of undefined prefetch.html:24
(anonymous function) prefetch.html:24
jQuery.event.dispatch jquery-1.10.2.js:5095
elemData.handle jquery-1.10.2.js:4766
jQuery.event.trigger jquery-1.10.2.js:5007
(anonymous function) jquery-1.10.2.js:5691
jQuery.extend.each jquery-1.10.2.js:657
jQuery.fn.jQuery.each jquery-1.10.2.js:266
jQuery.fn.extend.trigger jquery-1.10.2.js:5690
c.mixin.trigger typeahead.min.js:7
c.mixin._propagateEvent typeahead.min.js:7
proxy jquery-1.10.2.js:827
d.trigger typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin.open typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin._openDropdown typeahead.min.js:7
proxy jquery-1.10.2.js:827
d.trigger typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin._handleFocus typeahead.min.js:7
proxy jquery-1.10.2.js:827
jQuery.event.dispatch jquery-1.10.2.js:5095
elemData.handle
[{"value": "Abc Def", "tokens": ["Abc", "Def"], "name": "random1", "val2": "A", "val3": "B"}]