:(卡在这里。感谢任何帮助。
我的日志说
Started POST "/sign_in" for 127.0.0.1 at 2015-06-22 22:21:59 +0530 Processing by Users::SessionsController#create as HTML
但我尝试通过 jquery 请求 json
$.ajax({
type: 'POST',
contentType: "application/json",
url: 'http://localhost:3000/sign_in',
...
我的 Controller
class Users::SessionsController < Devise::SessionsController
respond_to :json
def create
respond_to do |format|
format.html {
.....
}
format.json {
.....
}
所以我在这里做错了什么,我需要将请求获取为“#create as JSON”
最佳答案
将 dataType
添加到您的 ajax:
type: 'POST',
contentType: "application/json",
url: 'http://localhost:3000/sign_in',
dataType: "json"
编辑
您正在将一些数据发布到操作create
,因此您应该只操作操作内的数据,然后将其重定向到其他地方......您不应该在操作中呈现任何信息创建,因为它是 post
而不是 get
。例如:
def create
# find your user based on the params
my_user = User.find(params[:id])
# you can manipulate the user or even save it in a session
# redirect to the action show for example
redirect_to user_path(my_user)
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @user }
end
end
这样,如果您使用 .json
访问 show
操作的 url,您将获得 json 格式的用户信息。
关于ruby-on-rails - Rails - 请求被视为 HTML 而不是 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30985898/