jQuery and Rails ajax request and response

ajax, javascript, jquery, ruby-on-rails

Solution

Try to use `stringify` your `data` or use `GET method` like,

data : JSON.stringify({name:"ravi",age:"31"}),

Full Code,

$.ajax({
     type: "POST",// GET in place of POST
     contentType: "application/json; charset=utf-8",
     url: "/room/test",
     data : JSON.stringify({name:"ravi",age:"31"}),
     dataType: "json",
     success: function (result) {
        //do somthing here
        window.alert("success!!");
     },
     error: function (){
        window.alert("something wrong!");
     }
});

Problem

Trying the basic stuff, request with data and response with data and print it with jQuery and Rails This is the front code. ``` $("#internal_btn").click(function() { //window.alert("clicked internal btn!"); $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "/room/test", //data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "', 'data3':'" + value3+ "'}", data: {name:"ravi",age:"31"}, dataType: "json", success: function (result) { //do somthing here window.alert("success!!"); }, error: function (){ window.alert("something wrong!"); } }); }); ``` in here, if the user clicks internal_btn this event happens and goes to the servide room/test action. ok this is fine. But I'm not sure how to send the data. If i run this, i have an error like this. ``` MultiJson::LoadError 795: unexpected token at 'name=ravi&age=31' ``` Can i know what the problem is? Also, is there are good example with this request and response with json format? I googled a lot, but not satisfied with the result :(

Original source