【发布时间】:2014-06-25 19:57:16
【问题描述】:
我使用下面的代码来GET所有机场数据(localhost:3000/api/v1/airports)所以Typeahead.js可以自动完成机场信息。
$(document).on('ready page:load', function () {
var airports = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('search_string'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: window.location.origin + "/api/v1/airports"
}
});
// kicks off the loading/processing of `local` and `prefetch`
airports.initialize();
// passing in `null` for the `options` arguments will result in the default
// options being used
$('.typeahead').typeahead(null, {
name: 'airports',
displayKey: 'search_string',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: airports.ttAdapter()
});
});
当我将它部署到 Heroku 并访问表单页面时,Chrome 中的控制台会返回以下内容:
GET http://localhost:3000/api/v1/airports.json net::ERR_EMPTY_RESPONSE application-b3c93a17005e4f5b078b985c7fa12088.js:3
它应该响应以下控制器:
module Api
module V1
class AirportsController < ApplicationController
skip_before_filter :verify_authenticity_token
respond_to :json
def index
respond_with(Airport.all)
end
def search
respond_with(Airport.pluck(:search_string))
end
def show
respond_with(Airport.find(params[:id]))
end
def create
@airport = Airport.new(airport.params)
if @airport.save
respond_to do |format|
format.json { render :json => @airport }
end
end
end
def update
@airport = Airport.find(params[:id])
if @airport.update(todo_params)
respond_to do |format|
format.json { render :json => @airport }
end
end
end
def destroy
respond_with Airport.destroy(params[:id])
end
end
end
end
这是路线的样子:
namespace :api, defaults: {format: :json} do
namespace :v1 do
get 'airports/search', to: 'airports#search', as: 'search'
resources :airports, :airlines, :countries
end
end
它确实适用于我的开发环境。有什么想法吗?
【问题讨论】:
-
向我们展示您希望响应的控制器,以及应该匹配的路由。
-
Heroku 上的本地主机????我不这么认为......
-
@TamerShlash 我已经用信息更新了问题。当我在本地运行代码时,它正在工作。
-
@RubyRacer 简单复制粘贴。
-
localhost:3000不应该在那里。您的 Heroku 部署应该有相应的主机名...。您是否在代码中的任何位置硬编码了localhost?也许清除您的浏览器缓存?
标签: javascript ruby-on-rails api get typeahead.js