我无法让预取选项在typeahead.js中工作。本地数据运行良好。对之前发布的问题的回答表明,缓存可能是一个问题,但我已经将ttl设置为0,并在Firefox和Chrome的私有浏览模式下进行测试。我遗漏了什么?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://twitter.github.com/typeahead.js/releases/latest/typeahead.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://twitter.github.com/typeahead.js/releases/latest/typeahead.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$('#searchText').typeahead([
{
name: 'terms',
//local: ["United States", "Canada"],
prefetch: 'http://twitter.github.io/typeahead.js/data/countries.json',
ttl: 0
}
]);
});
</script>
<body>
<div class="container">
<input class="typeahead" type="text" id="searchText">
</div>
</body>
</html>发布于 2013-08-09 10:41:43
您应该始终检查console (Firebug / Chrome工具),当某些东西“不起作用”时,它是您在使用JS时最好的朋友。
在这种情况下,并不是prefetch不起作用。但是Github不允许您从不同的域获取那个json。如果您检查您的控制台,您将看到以下错误:
XMLHttpRequest cannot load http://twitter.github.io/typeahead.js/data/countries.json. Origin http://yourdomain is not allowed by Access-Control-Allow-Origin.因此,如果您想让这个示例在本地工作,您应该下载该JSON文件并在本地设置它,然后将URL更改为:
$(document).ready(function(){
$('#searchText').typeahead({
name: 'terms',
prefetch: '/data/countries.json', // go to http//yourdomain/data/countries to make sure the path is correct
ttl: 0
});
});希望能帮上忙。
https://stackoverflow.com/questions/18128279
复制相似问题