Ruby on Rails
Tuesday, August 13, 2013
how i define my controller :
def show
@user=User.find(params[:id])
respond_to do |format|
format.json { render json: @user.to_json, :only => [:name, :email]}
end
end
how i define my ajax function:
function FunAjax()
{
$.ajax({
type: "POST",
url: "http://ip:3000/users/1.json",
data: "begin=1&end=9",
dataType: "json",
timeout : '50000',
error : function(){
alert ('server error');
},
beforeSend: function(){
$("#span_content").text("processing...");
},
success:function(data){
if(data === null){
alert('no data');
}else{
var div_cont ="";
$.each(data,function(idx,arr_item){
div_cont = div_cont +"<br/>key:"+idx+"value:"+ arr_item;
}),
$("#span_content").html(div_cont);
}
}
});
};
$(document).ready(function () {
alert('start');
});
then i browse the url:http://ip:3000/users/1.json
it will return a string like this:
{"name":"fang","email":"fang@fang.com"}
but the ajax function won`t work.
please tell me how can i exchange data between the server and the browser?
-- def show
@user=User.find(params[:id])
respond_to do |format|
format.json { render json: @user.to_json, :only => [:name, :email]}
end
end
how i define my ajax function:
function FunAjax()
{
$.ajax({
type: "POST",
url: "http://ip:3000/users/1.json",
data: "begin=1&end=9",
dataType: "json",
timeout : '50000',
error : function(){
alert ('server error');
},
beforeSend: function(){
$("#span_content").text("processing...");
},
success:function(data){
if(data === null){
alert('no data');
}else{
var div_cont ="";
$.each(data,function(idx,arr_item){
div_cont = div_cont +"<br/>key:"+idx+"value:"+ arr_item;
}),
$("#span_content").html(div_cont);
}
}
});
};
$(document).ready(function () {
alert('start');
});
then i browse the url:http://ip:3000/users/1.json
it will return a string like this:
{"name":"fang","email":"fang@fang.com"}
but the ajax function won`t work.
please tell me how can i exchange data between the server and the browser?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/de3cc05f-c577-44dd-b9e7-de1c9ebf1b32%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment