简体   繁体   English

如何在Rails中将JSON响应从控制器传递给Ajax调用

[英]How to pass json response from controller to ajax call in rails

this is my ajax call section in js file : 这是我在js文件中的ajax调用部分:

$("#sign_submit").click(function(){
            email_id = $("#user_email").val();
            password = $("#user_password").val();

            data =  {email: email_id, password: password };
            url = user/sign_in';

            $.ajax({
                url: url,
                data:  data,
                cache: false,
                type: "post",
                success: function(data){
                    console.log(data);
                }
              });
});

This is my controller Action : 这是我的控制器动作:

  def create
    @user = User.find_by_email(params['email'])
    if @user.nil?
      respond_to do |format|
        format.json {render :json => {:response => 'User is invalid'} }     #how to pass json response here. 
      end
    end

    if @user.check_valid_user?(params['password'])
      set_dashboard_location
      set_current_user(session[:customer_id])
      render js: %(window.location.href='#{session["spree_user_return_to"]}')
    else
       #redirect_to '/' and return 
       #how to psas json response here.
    end
  end

In in create actions (else part), I need to pass json response regarding( invalid user/ invalid user credentials) to my ajax call, which I can alert user on the View Page. 在创建动作(其他部分)中,我需要将有关(无效用户/无效用户凭据)的json响应传递给我的ajax调用,以便在查看页面上提醒用户。

I have difficulty to understand where is the problem but I will do my best to help you. 我很难理解问题出在哪里,但我会尽力为您提供帮助。

The basic command to render json from a Rails controller is 从Rails控制器渲染JSON的基本命令是

render json: @user

for example. 例如。 I saw that you wrote something like that but inside a certain block. 我看到您在某个块中写了类似的内容。 When you write 当你写

respond_to do |format|
  format.json { render json: @foo }
end

The json rendering will only occur when the url for the query finishes by .json (The format of the query) So here url = user/sign_in.json will render json but url = user/sign_in will assume the format is html (the default format, that you can change in your routes definitions) and won't render anything. 仅当查询的网址以.json(查询的格式) url = user/sign_in.json才会进行json呈现。因此,此处url = user/sign_in.json将呈现json,但url = user/sign_in将假定格式为html(默认值格式,您可以在路线定义中进行更改),并且不会呈现任何内容。

If you need to render json no matter the url. 如果您需要渲染json,无论网址如何。 Do not put it in the block respond_to . 请勿将其放在respond_to块中。 If you need 2 different behaviors if it's an ajax call or another call, use the url foo/bar.json 如果是ajax调用或其他调用,如果需要两种不同的行为,请使用url foo/bar.json

If you run rake routes in your rails root folder, you will see urls like /users/sign_in(.:format) , that means that /users/sign_in.toto will set the :format to toto 如果在rails根文件夹中运行rake routes ,则会看到类似/users/sign_in(.:format) url,这意味着/users/sign_in.toto会将:format设置为toto

I hope it was clear ! 我希望这很清楚! If you prefer to set a default format in your routes : 如果您希望在路线中设置默认格式,请执行以下操作:

How to set the default format for a route in Rails? 如何在Rails中设置路线的默认格式?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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