我正在尝试将typeahead.js与我的Go程序集成。它似乎没有工作。这是我到目前为止所做的:
目录结构:
/hello/
public/
js/
countries.json
form.js
typeahead.min.js
templates/
form.html
hello.go
countries.json:
["Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla"]
form.js
var countries = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: 'hello/public/js/countries.json',
filter: function(list) {
return $.map(list, function(country) { return { name: country }; });
}
}
});
countries.initialize();
$('#prefetch .typeahead').typeahead(null, {
name: 'countries',
displayKey: 'name',
source: countries.ttAdapter()
});
typeahead.min.js:
来自http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.min.js
form.html
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="[absolute path to]/hello/public/js/form.js"></script>
<script src="[absolute path to]/hello/public/js/typeahead.min.js"></script>
<script type="text/javascript"></script>
<body>
<form method="post" action="/join" id="loginForm">
<div id="prefetch">
<input class="typeahead" type="text" placeholder="Countries">
</div>
<button type="submit">Join</button>
</form>
</body>
</html>
hello.go中的文件服务器:
http.Handle(“/ public / js /”,http.StripPrefix(“/ public / js”,http.FileServer(http.Dir(filepath.Join(cwd,“/ github.com / hello / hello /)公共/ JS“)))))
控制台出错:
Uncaught SyntaxError: Unexpected token <
typeahead.min.js:1 Uncaught SyntaxError: Unexpected token <
我引用了:
http://runnable.com/UlXgeluakNULAAAv/create-an-autocomplete-input-box-with-typeahead-js-for-jquery-and-javascript http://plugins.upbootstrap.com/bootstrap-ajax-typeahead/
我一直试图解决这个问题。我可以看到表单显示,似乎正在从CDN导入bootstrap和jquery,但没有下拉自动完成建议。
我真的很感激帮助!!或者如果您需要更多信息,请告诉我。谢谢!
答案 0 :(得分:0)
您所得到的错误&#34;&lt;&#34;在typeahead.min.js:1听起来像你的后端返回html而不是javascript。
您可能想检查路线。
你没有显示hello.go,所以这只是猜测,但我认为html中的路径应该是&#34; / js /..." ;;而不是绝对路径。