Trying the basic stuff,
在最基本的东西,
request with data and response with data and print it with jQuery and Rails
用数据请求,用数据响应,用jQuery和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
在这里,如果用户单击internal_btn,此事件将发生并转到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?
另外,这个请求和响应是否有json格式的好例子?
I googled a lot, but not satisfied with the result :(
我用谷歌搜索了很多次,但对结果并不满意。
1 个解决方案
#1
3
Try to use stringify
your data
or use GET method
like,
尝试使用stringify你的数据或者使用GET方法,
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!");
}
});
#1
3
Try to use stringify
your data
or use GET method
like,
尝试使用stringify你的数据或者使用GET方法,
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!");
}
});