【发布时间】:2013-08-04 10:35:22
【问题描述】:
我正在尝试从触发 AJAX 调用中获取成功函数。我知道它工作正常,因为我正在访问我自己的 API,我可以看到它正在正确访问 URL,并且服务器正在输出 HTTP 200。
我认为这是因为服务器正在输出 json,所以我尝试在 AJAX 调用中考虑这一点,但仍然无法成功功能。这是我的代码
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
【问题讨论】:
-
我认为这与 AJAX 调用无关。答案是警告可以忽略不计。
-
你知道
dataType的真正含义吗?您是否尝试过添加error回调? -
GET 不应该有 Content-Type
-
我正在查看 dataType,我认为这就是我想要的。我期待返回
User模型的 JSON 对象。在网上搜索后,我也将其视为一种解决方案。但它并没有发生在我的情况下。
标签: javascript jquery ruby-on-rails ajax