Javascript Typeahead.js预取选项不起作用
我无法在typeahead.js中使用预回迁选项。本地数据运行良好。对之前发布的问题的回答表明缓存可能是一个问题,但我已经将ttl设置为0,并且正在Firefox和Chrome中以私人浏览模式进行测试。我错过了什么Javascript Typeahead.js预取选项不起作用,javascript,typeahead.js,Javascript,Typeahead.js,我无法在typeahead.js中使用预回迁选项。本地数据运行良好。对之前发布的问题的回答表明缓存可能是一个问题,但我已经将ttl设置为0,并且正在Firefox和Chrome中以私人浏览模式进行测试。我错过了什么 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="http://twitter.github.com/typeahead.js/rel
<!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>
$(文档).ready(函数(){
$(“#searchText”)。请提前键入([
{
名称:'条款',
//本地:[“美国”、“加拿大”],
预取:'http://twitter.github.io/typeahead.js/data/countries.json',
ttl:0
}
]);
});
当某些东西“不工作”时,您应该始终检查控制台(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
});
});
希望有帮助。当某些东西“不起作用”时,您应该始终检查控制台(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
});
});
希望能有帮助。@PauloTomé不要那样使用反勾号。它们不是用来突出显示的。@Paulotmé不要这样使用背景标记。它们不是用来突出显示的。这确实有帮助,谢谢。无论出于何种原因,我在Firebug的控制台部分没有看到错误消息,但当我切换到Chrome时,我确实看到了这条消息。谢谢你的帮助,真的很有帮助,谢谢。无论出于何种原因,我在Firebug的控制台部分没有看到错误消息,但当我切换到Chrome时,我确实看到了这条消息。谢谢你的帮助。