1

我正在尝试将 json 对象从 javascript 发送到 rails 控制器,但我不断收到此错误

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

我正在使用 yajl、rails 3.1、jquery 1.6。

在 gemfile gem 'yajl-ruby'中设置Yajl

配置/应用程序.rb

 require 'yajl/json_gem'

我的代码

ajax函数

    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)
4

1 回答 1

1

您应该使用该请求发送 json 数据,而不是参数字符串。

...
data: JSON.stringify(myobj),
...
于 2012-12-06T15:29:30.443 回答