katie
katie

Reputation: 2951

Invalid string in json text while trying to send json object to a controller

I am trying to send json object from javascript to rails controller and i keep getting this error

  MultiJson::DecodeError (lexical error: invalid string in json text.
                                   tag=hello
                 (right here) ------^

I am using yajl,rails 3.1,jquery 1.6.

Yajl setup in gemfile gem 'yajl-ruby'

config/application.rb

 require 'yajl/json_gem'

My code

ajax function

    var myobj={"tag":"hello"};
    $.ajax({
    url: 'ips/create',
    type: 'post',
    contentType: 'application/json; charset=UTF-8',
    accept: 'application/json',
    dataType: 'json',
    data:$.param(myobj),

    success: function(res) {
        if (res.ImportResponse !== void 0) {
            console.log('Success: ' + res);
        } else if (res.Fault !== void 0) {
            console.log('Fault: ' + res);
        }
    },
    error: function() {
        console.error('error!!!!');
    }
   });

*In controller*

     parser = Yajl::Parser.new
     hash = parser.parse(request.body.read)

Upvotes: 1

Views: 1057

Answers (1)

Kevin B
Kevin B

Reputation: 95027

You should be sending json data with that request, not a param string.

...
data: JSON.stringify(myobj),
...

Upvotes: 1

Related Questions