简体   繁体   中英

How to send json response from controller to jQuery and inspect it?

I have this problem : I'm checking user email in controller and sending json successfull response if it is already taken and add css styling of input, also I need to prevent submit and add some message.

Here is my checkemail action ( used this article - http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery ):

def checkemail
  if !User.where('email = ?', params[:email]).empty?
    format.jsonr do
      render :json => { 
        :status => :ok, 
        :message => "Success!",
      }.to_json
    end
  end
end

and in my routes:

         checkemail GET    /checkemail(.:format)

          match "/checkemail", to: 'edit_user#checkemail'

and my jQuery:

   $('#user_email').focusout(function() {
    $.getJSON('/checkemail', { email: $('#user_email').val() }, function(data) {
        $('#user_email').addClass("error");
      });
   });

my HTML input code:

     <input id="user_email" name="user[email]" size="30" type="email" value="">

from mime_type initializers file:

    Mime::Type.register_alias "application/json", :jsonr, %w( text/x-json )

  Request : GET /checkemail?user@mail.com HTTP/2.0

  Response(RAW tab) : 
  Missing template edit_user/checkemail

  JSON tab is empty

Why format.jsonr ? Seems like a typo to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM