1

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)
1
  • well, you set your contentType to json and instead sent a param string. That error makes complete sense. Commented Dec 6, 2012 at 15:27

1 Answer 1

1

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

...
data: JSON.stringify(myobj),
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the error is gone , now i just get missing template erreor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.