【发布时间】:2012-01-10 21:52:53
【问题描述】:
{问题已更新}
我想向安装了 rails 的服务器发送一些数据。我的数据是 JSON 格式,例如:
var JSONObject= {
table1: {
title: "Title of text",
url: "Url of text"
}
}
我在 cilent 中使用以下代码:
$.ajax({
type: "POST",
url : "http://webadress.com/table1",
cache: false,
data: JSONObject,
statusCode: {
200:function() { alert("200"); },
202:function() { alert("202"); }
},
success: function() { alert("Data Send!");},
error: function(xhr){ alert("The error code is: "+xhr.statusText);}
});
在cilent中,存在以下代码:
def create
@table1= Table1.new(:title => params[:title], :url => params[:url])
respond_to do |format|
if @table1.save
format.html { redirect_to(@table1, :notice => 'User was successfully created.') }
format.xml { render :xml => @table1, :status => :created, :location => @table1}
format.json { render :json => @table1}
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end
但它不起作用。 如何获取数据并将其存储在数据库中。我的意思是如何将每个元素放入不同的列?
【问题讨论】:
标签: jquery ruby-on-rails ruby ajax json