2016-08-13 1 views
0

私は両方のapiとWebアプリケーションのためにdeviseを使用しています。 Webアプリは完璧に動作します。 ajaxでユーザーを作成しようとすると、機能しません。deviseは、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 

ここではAjaxのリクエストがあります私はを使用しています210それはそれとは関係がないはずです。これをどうやって解決するのですか?

答えて

0

これは助けることができる:

だけ削除してくださいJSON.stringifyだけ{user: {email: '[email protected]', password: 'password', password_confirmation: 'password'}}を使用しています。

OR

下記のリンクは、JSONデータ形式とHTMLのフォームを送信する方法を示しています。これはWC3標準です。私はこれがあなたの問題を解決すると思います。以下のリンクに従ってください。

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

+0

ありがとうございますが、フォームを送信しようとしていません。私は私のサーバーにjson要求を送ることができる人が必要です。 jsonがサーバーに正しく当たっているようです。レールが使っている方法と関係していると思います。 – Philip7899

+0

あなたが送信したデータに電子メール属性が存在するかどうかを調べ、テストします。 –

+0

どうすればよろしくお願いします。 – Philip7899

関連する問題