我正在为api和web应用程序使用设计。网络应用程序完美运行。当我尝试使用ajax创建用户时,它不起作用。这是ajax请求:
$.ajax({
type: "POST",
url: 'http://localhost:3000/api/users',
data: JSON.stringify({user: {email: 'demouser@demo.com', password: 'password', password_confirmation: 'password'}}),
dataType: "json",
contentType: "application/json; charset=utf-8"
});
返回以下responsetext:
"{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}"
以下是日志:
Started POST "/api/users" for ::1 at 2016-08-13 15:44:59 -0400
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
application controller
yolo
Processing by RegistrationsController#create as JSON
Parameters: {"user"=>{"email"=>"demouser@demo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"deafdssadfaamousera2129@demo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}
IN CREATE
(0.1ms) begin transaction
(0.1ms) rollback transaction
Completed 422 Unprocessable Entity in 40ms (Views: 1.3ms | ActiveRecord: 0.5ms)
以下是路线:
devise_for :users
namespace :api, defaults: { format: :json } do
devise_for :users, controllers: {registrations: "registrations"}, :defaults => { :format => :json }
end
我使用skip_before_filter :verify_authenticity_token
所以它不应该与此有任何关系。我该如何解决这个问题?
答案 0 :(得分:0)
这可以提供帮助:
请仅删除JSON.stringify
并仅使用{user: {email: 'demouser@demo.com', password: 'password', password_confirmation: 'password'}}
。
或强>
以下链接显示了如何使用json数据格式提交html表单。这是WC3标准。我认为这解决了你的问题。请点击以下链接。