最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery and Rails ajax request and response - Stack Overflow

programmeradmin0浏览0评论

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 :(

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 :(

Share Improve this question asked Nov 20, 2013 at 4:47 CannaCanna 3,8145 gold badges29 silver badges35 bronze badges 2
  • can you show us your controller action that respond json data – medBouzid Commented Nov 20, 2013 at 4:56
  • Thanks for the response. I didn't make the controller action. First i'm trying to request to the controller and get the pararmeters from the front. – Canna Commented Nov 20, 2013 at 5:08
Add a ment  | 

1 Answer 1

Reset to default 4

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!");
     }
});
发布评论

评论列表(0)

  1. 暂无评论