Can't get JSON data from Rails API

末鹿安然 提交于 2019-12-24 01:44:48

问题


I make a get request to http://localhost:3000/cars/1 but server does not reply json data. It says like that

Started GET "/api/cars/1" for 127.0.0.1 at 2016-10-07 12:47:34 +0600
Processing by Api::V1::CarsController#show as HTML
Parameters: {"id"=>"1"}
User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."uid"       = $1 LIMIT $2  [["uid", "sahidul03@gmail.com"], ["LIMIT", 1]]
Car Load (0.6ms)  SELECT  "cars".* FROM "cars" WHERE "cars"."id" = $1    LIMIT $2  [["id", 1], ["LIMIT", 1]]
User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
Started GET "/" for 127.0.0.1 at 2016-10-07 12:47:34 +0600
Processing by Rails::WelcomeController#index as HTML
Parameters: {"internal"=>true}
(2.9ms)  BEGIN
Rendering /home/sahidul/.rvm/gems/ruby-2.2.3/gems/railties- 5.0.0.1/lib/rails/templates/rails/welcome/index.html.erb
Rendered /home/sahidul/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/templates/rails/welcome/index.html.erb (3.9ms)
Completed 200 OK in 11ms (Views: 10.2ms | ActiveRecord: 0.0ms)


(6.8ms)  COMMIT
Completed 200 OK in 25ms (Views: 1.3ms | ActiveRecord: 11.8ms)

My controller method is like that

 def show
   car= Car.find(params[:id])
   render json: car
 end

Front-end request is

  function getCar(id) {
        return $http.get($auth.domain + '/api/cars/' + id);
    }

I use devise_token_auth gem in back-end and ng-token-auth in front-end


回答1:


Append .json to the URL

$http.get($auth.domain + '/api/cars/' + id + '.json');

Hope that helps!



来源:https://stackoverflow.com/questions/39911429/cant-get-json-data-from-rails-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!