如何将JSON响应传递回客户端 [英] How to pass json response back to client

查看:171
本文介绍了如何将JSON响应传递回客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ruby on rails代码中,我想将json响应发送回客户端.由于我不熟悉红宝石,所以我不知道该怎么做.如果数据未保存到数据库中并且数据成功保存,我想将error = 1 and success = 0作为json数据发送,则应发送success = 1 and error = 0.

这是我的控制器

class ContactsController < ApplicationController
  respond_to :json, :html
  def contacts
    error = 0
    success = 1

    @contacts = Contact.new(params[:contact])

    if @contacts.save
      respond_to do |format|
        format.json { render :json => @result.to_json }
      end
    else
      render "new"
    end
  end
end

这是我的JavaScript代码

$('.signupbutton').click(function(e) {
        e.preventDefault();
        var data = $('#updatesBig').serialize();
        var url = 'contacts';
        console.log(data);
        $.ajax({
            type: 'POST',
            url: url,
            data: data,
            dataType: 'json',
            success: function(data) {
                console.log(data);
            }
        });
    });

解决方案

还有很多其他优雅的方法,但这是正确的:

class ContactsController < ApplicationController

  def contacts
    @contacts = Contact.new(params[:contact])
    if @contacts.save
       render :json => { :error => 0, :success => 1 }
    else
       render :json => { :error => 1, :success => 0 }
    end 
  end

end

还添加一条路由到routes.rb.如果您需要使用html响应,则必须包含response_to | format |.

In my ruby on rails code I want to send back json response to client. Since I am new to ruby on rails I do not know how can I do this. I want to send error = 1 and success = 0 as json data if data does not save to database and if it successfully saves that it should send success = 1 and error = 0 Please see my code below

here is my controller

class ContactsController < ApplicationController
  respond_to :json, :html
  def contacts
    error = 0
    success = 1

    @contacts = Contact.new(params[:contact])

    if @contacts.save
      respond_to do |format|
        format.json { render :json => @result.to_json }
      end
    else
      render "new"
    end
  end
end

here is my javascript code

$('.signupbutton').click(function(e) {
        e.preventDefault();
        var data = $('#updatesBig').serialize();
        var url = 'contacts';
        console.log(data);
        $.ajax({
            type: 'POST',
            url: url,
            data: data,
            dataType: 'json',
            success: function(data) {
                console.log(data);
            }
        });
    });

解决方案

There are tons of other elegant ways, but this is right:

class ContactsController < ApplicationController

  def contacts
    @contacts = Contact.new(params[:contact])
    if @contacts.save
       render :json => { :error => 0, :success => 1 }
    else
       render :json => { :error => 1, :success => 0 }
    end 
  end

end

Add also a route to routes.rb. If you need to use html response you have to include respond_to do |format|.

这篇关于如何将JSON响应传递回客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆