我一直试图让json将json返回给ajax请求,但它不断发回html。我跟着this(以及其他,我真的查看了2个谷歌搜索页面上的每个链接)教程,但它仍然发送html。这是我改变的地方和地点:
配置/初始化/ devise.rb:
添加:config.http_authenticatable_on_xhr = false
创建了app / controllers / sessions_controller:
class SessionsController < Devise::SessionsController
respond_to :json
end
创建了app.controllers / registrations_controller:
class RegistrationsController < Devise::RegistrationsController
respond_to :json
end
在config / routes.rb
下添加
devise_for :users, :controllers => {
sessions: 'sessions',
registrations: 'registrations'
}
我想是的。但我一直在回复html。我应该删除与设计相关的部分视图吗?
以下是请求的内容:
开始发布&#34; / users / sign_in&#34; for :: 1 at 2016-06-17 09:01:19 -0400 由SessionsController处理#create as / 参数:{&#34;用户&#34; =&gt; {&#34;电子邮件&#34; =&gt;&#34;&#34;,&#34;密码&#34; =&gt;&#34; [过滤]&#34;},&#34; authenticity_token&#34; =&gt;&#34; HDlnoiGZXXoarFjdlfxZL6AF0EY8Xf1K5mRwceVmVw647lG + NPJxDYstXLhiH4BGGwmNrrX8U5gZD8B3IfhS + w ==&#34;}
答案 0 :(得分:0)
我在ajax请求中缺少dataType
属性,在将其设置为`application / json'之后,它已经工作了。
$.ajax({
method: "POST",
url: "/users/sign_in",
dataType: 'application/json', <--- here
headers: {
'X-CSRF-Token': csrfTokenContent
},
data: {
user: {
email: email,
password: password,
},
authenticity_token: csrfTokenContent
}
})