ruby on rails - Ajax response from controller -


my javascript send ajax request invoke controller function, controller function response ajax request. problem in controller response part.

my javascript send ajax request controller:

var xmlhttp = new xmlhttprequest();     xmlhttp.onreadystatechange=function(){         if (xmlhttp.readystate==4 && xmlhttp.status==200){           //render response @cars here         }     }     xmlhttp.open("get","/cars/reserved_cars/"+customer_id,true);     xmlhttp.send(); 

so, above ajax request invoke carscontroller's reserved_cars function customer_id parameter.

my carscontroller:

class carscontroller < basecontroller  def reserved_cars     customer_id=params[:customer_id]      @cars = car.getcars(customer_id)   end ...   end 

my controller cars query car model customer_id.

everything works fine, don't know how return @cars in controller response of ajax request javascript (the place in javascript commented "//render response @cars here")

so, how @cars response in javascript?

thanks guys, figured out solution.

i first convert @cars string cars_str,

then use render :json => cars_str in controller,

in javascript can cars_str string xmlhttp.responsetext


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -