【问题标题】:Why is this GET request to a Rails API not working?为什么这个对 Rails API 的 GET 请求不起作用?
【发布时间】: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


【解决方案1】:

您的资产,特别是您的 javascript,包含指向 http://localhost:3000 的硬编码链接,用于 GET/json 请求,我猜来自您的 javascript 代码中的一些测试。

它被编译并保留在那里。

在生产环境中,您对资产所做的更改不适用于已编译的资产(它们已编译...)。

编译资产的唯一目的是加快加载速度......

因此,每次更改后,您必须重新编译资产以使更改生效。这是一个乏味的过程(我重复一遍,仅在标准生产环境中),但如果您在生产环境中运行仍在开发中的应用程序(这种情况发生的频率比您想象的要多),这是必要的。

这就是重新编译资产并重新启动应用程序服务器可以解决问题的原因。

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2018-04-10
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2018-05-15
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多