Rails:通过Ajax传递参数 [英] Rails: Pass Params Through Ajax

查看:121
本文介绍了Rails:通过Ajax传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过javascript将参数传递回服务器.此刻,我将它们传递给javascript,如下所示:

I need to pass params through javascript back to the server. At the moment, I pass them into javascript like so:

sendParams("<%= params[:q].to_json %>");

然后像这样将它们发送回去:

And then send them back like this:

function sendParams(q){
  $.ajax({
    url: '/mymodel/myaction',
    type: 'post',
    data: {'q':q},
    contentType: 'json'
  });
}

在我的控制器中,我尝试像使用其他任何参数一样使用它们:

In my controller, I try to use them like I would any other params:

MyModel.where(params[:q])

但是即使Firebug在POST选项卡中显示了此参数,参数也变空了:

But the params are coming back empty, even though firebug shows this in the POST tab:

q=%7B%26quot%3Bc%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Ba%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Btitle%26quot%3B%7D%7D%2C%26quot%3Bp%26quot%3B%3A%26quot%3Bcont%26quot%3B%2C%26quot%3Bv%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bvalue%26quot%3B%3A%26quot%3B2%26quot%3B%7D%7D%7D%7D%2C%26quot%3Bs%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Bvotes_popularity%26quot%3B%2C%26quot%3Bdir%26quot%3B%3A%26quot%3Bdesc%26quot%3B%7D%7D%7D

您知道为什么where子句不会处理此信息吗?我该怎么做才能使参数Rails再次可读?

Any idea why this information isn't getting processed by the where clause? What can I do to make the params Rails readable again?

更新:

Started POST "/publications/search?scroll=active&page=6" for 127.0.0.1 at 2013-0
2-12 22:55:24 -0600
Processing by PublicationsController#index as */*
Parameters: {"scroll"=>"active", "page"=>"6"}

更新2:

问题显然是由于contentType引起的.当我删除它时,然后q作为Rails参数发送.不幸的是,q仍然是JSON,导致错误:

The problem is apparently stemming from contentType. When I remove it, then q is sent as a Rails parameter. Unfortunately, q is still in JSON, resulting in the error:

undefined method `with_indifferent_access' for #<String:0x686d0a8>

如何将JSON转换为参数哈希?

How can I convert JSON to a params hash?

推荐答案

要解决此问题,需要解决一些问题.首先,q并没有作为参数发送到Rails,即使它正在发布.原因是因为它被视为JSON数据而不是参数.我通过删除以下行来解决此问题:

There were a couple of issues that needed to be resolved for this to work. First, q wasn't being sent as a parameter to Rails, even though it was posting. The reason was because it was being treated as JSON data rather than as a parameter. I fixed this by removing the line:

contentType: 'json'

此后,AJAX正确发送了"q",但Rails像在JSON中那样使用它时遇到了麻烦.我必须用ActiveSupport::JSON.decode进行解析,但这引发了737: unexpected token错误.我通过(JSONlint)[http://jsonlint.com/]运行了代码,结果发现所有引号均已转义.

After that, the AJAX properly sent 'q', but Rails had trouble using it as it was in JSON. I had to parse it with ActiveSupport::JSON.decode, but this was throwing a 737: unexpected token error. I ran the code through (JSONlint)[http://jsonlint.com/], and it turns out that all the quotation marks had been escaped.

从那里开始,有两种解决方案.显而易见的方法是像这样使用.html_safe:

From there, there were two solutions. The obvious one was to use .html_safe like so:

sendParams("<%= params[:q].to_json.html_safe %>");

但这在用户输入引号时引起问题.更安全的选择是像这样将转义的HTML实体传递回Rails后对其进行解码:

But this caused problems when the user inputed quotes. The safer alternative was to decode the escaped HTML entities after they were passed back to Rails like so:

ActiveSupport::JSON.decode(CGI.unescapeHTML(params[:q]))

这成功了.

这篇关于Rails:通过Ajax传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆