[英]Respond to ajax request with a string in Rails
I am trying to use respond_with in a Rails 4 controller to respond to an ajax request from an AngularJS app. 我正在尝试在Rails 4控制器中使用response_with来响应来自AngularJS应用的ajax请求。 I just want to return a json web token (string), not an active record object.
我只想返回json网络令牌(字符串),而不是活动的记录对象。 How do I do this?
我该怎么做呢?
I am getting very weird errors about a url being undefined or location being nil. 我收到有关网址未定义或位置为零的非常奇怪的错误。 Any help will be greatly appreciated.
任何帮助将不胜感激。
Sessions Controller: 会话控制器:
class SessionsController < ApplicationController
respond_to :json
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
#login
token = log_in(user)
else
#error
end
respond_with(token)
end
end
Route: 路线:
post 'sessions/create' => 'sessions#create', defaults: { format: :json }
Latest error: 最新错误:
undefined method 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9_url' for #<SessionsController:0x007ff8d0a42f10>
我通过以下方法进行工作:
render json: token, status: 200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.