我正在尝试从 AJAX 调用中获取成功功能。我知道它工作正常,因为我正在访问我自己的 API,并且我可以看到它正在正确访问 URL,并且服务器正在输出 HTTP 200。
我认为这是因为服务器正在输出 json,所以我尝试在 AJAX 调用中考虑这一点,但成功功能仍然无法正常工作。这是我的代码
阿贾克斯
$.ajax('http://localhost:3000/api/users/show/:id', {
type: 'GET',
dataType: 'json',
contentType: "application/json",
data: {
id: 1
},
success: function(response) {
return alert("Hey");
}
});
api方法
class UsersController < ApplicationController
respond_to :json
def show
respond_with User.find(params[:id])
end
end
服务器日志
Started GET "/api/users/show/:id?id=1" for 127.0.0.1 at 2013-08-02 20:36:42 -0700
Processing by MainController#index as JSON
Parameters: {"id"=>"1", "path"=>"api/users/show/:id", "main"=>{}}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Rendered main/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 146ms (Views: 144.3ms | ActiveRecord: 0.5ms)
[2013-08-02 20:36:42] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true