2016-08-13 18 views
0

我正在使用設計的api和web應用程序。該Web應用程序完美地工作。當我嘗試用ajax創建用戶時,它不起作用。下面是Ajax請求:返回設計創建用戶不使用json。這是說,電子郵件是空白,當它存在

$.ajax({ 
    type: "POST", 
    url: 'http://localhost:3000/api/users', 
    data: JSON.stringify({user: {email: '[email protected]', 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"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"[email protected]", "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

這可以幫助:

請只是刪除JSON.stringify,並且只使用{user: {email: '[email protected]', password: 'password', password_confirmation: 'password'}}

OR

下面的鏈接顯示如何提交HTML表單使用JSON數據格式。這是WC3標準。我認爲這解決了你的問題。請按照下面的鏈接。

https://darobin.github.io/formic/specs/json/

+0

謝謝,但我沒有試圖提交表格。我需要人們能夠發送json請求到我的服務器。 json似乎正確地擊中了服務器。我認爲它與軌道使用它的方式有關。 – Philip7899

+0

使用pry,然後測試您發送的數據中是否存在電子郵件屬性。 –

+0

你如何使用pry – Philip7899

相關問題