【问题标题】:Sencha seems to not like Rails' jsonSencha 似乎不喜欢 Rails 的 json
【发布时间】:2011-04-14 17:00:24
【问题描述】:

我有一个 Rails 控制器,它将一些模型渲染为 json。

 @events = Event.all
 respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @events }
  format.json { render :json => { :results => @events } }
 end

输出如下所示:

{
    "results":
    [
        {
            "name":"test",
            "created_at":"2011-03-23T13:32:57Z",
            "latitude":60.0,
            "location":null,
            "updated_at":"2011-04-14T16:38:54Z",
            "id":1,
            "longitude":30.0,
            "takes_place_at":"2011-03-23T13:32:00Z"
        },
        {
            "name":"x",
            "created_at":"2011-03-23T13:36:44Z",
            "latitude":60.0,
            "location":null,
            "updated_at":"2011-03-23T13:36:44Z",
            "id":2,
            "longitude":30.0,
            "takes_place_at":"2011-03-23T13:36:00Z"
        },
        {
            "name":"Gruempi",
            "created_at":"2011-03-24T14:00:32Z",
            "latitude":60.0,
            "location":null,
            "updated_at":"2011-04-14T16:39:47Z",
            "id":3,
            "longitude":30.0,
            "takes_place_at":"2011-03-24T14:00:00Z"
        },
        {
            "name":"ba",
            "created_at":"2011-04-10T22:40:07Z",
            "latitude":60.0,
            "location":"12,
            14",
            "updated_at":"2011-04-10T22:40:07Z",
            "id":4,
            "longitude":30.0,
            "takes_place_at":"2011-04-10T22:36:00Z"
        },
        {
            "name":"sfasdfs",
            "created_at":"2011-04-10T22:44:55Z",
            "latitude":60.0,
            "location":"we're try to find you ...",
            "updated_at":"2011-04-10T22:44:55Z",
            "id":5,
            "longitude":30.0,
            "takes_place_at":"2011-04-10T22:42:00Z"
        },
        {
            "name":"asdfsad",
            "created_at":"2011-04-10T22:55:03Z",
            "latitude":60.0,
            "location":"we're try to find you ..asfd.",
            "updated_at":"2011-04-10T22:55:03Z",
            "id":6,
            "longitude":30.0,
            "takes_place_at":"2011-04-10T22:54:00Z"
        }
    ]
}

我尝试使用 sencha/extjs 使用该服务:

refresh = function() {
  Ext.util.JSONP.request({
    url: 'http://localhost:3000/search/by_date.json',
    callbackKey: 'callback',
    params: {
      year:"2011",
      month:"04",
      day:"10",
      uniqueify: Math.random()
    },
    callback: function(data) {
      var events = data.results;
      timeline.update(events);
    }
  });
};

而 sencha 只是不解析它。同时,它适用于 Twitter 等 API 调用。

任何想法我做错了什么?或者我怎样才能找到错误?

【问题讨论】:

    标签: ruby-on-rails json sencha-touch extjs


    【解决方案1】:

    如果您正在执行 JSONP 请求,则需要将返回的 JSON 包装在 GET 请求中指定为参数的函数中,在您的情况下为“回调”。 Sencha touch 将在内部处理函数命名,但您需要注意您传入的 callbackKey 选项,以便您的服务器能够正确响应。

    当请求http://localhost:3000/search/by_date.json?callback=jsonP2343243243 时,预期的响应应该包含在回调参数指定的函数中。该 GET 请求应生成以下 JSON:

    jsonP2343243243({ "results": [ ... ] });

    这将导致该函数在被浏览器解释时被调用,然后将调用 AJAX 的回调。在 Rails 中,您需要将渲染更改为:

    导轨

    format.js { render :js => params[:callback] + "(" + { :results => @events }.to_json + ");"} 
    

    导轨 > 3.0

    format.js { render :json => { :results => @events },  :callback => params[:callback] }
    

    【讨论】:

    • render :json => { :results => @events }, :callback => params[:callback] 它内置在 Rails 3 中
    • 谢谢。 format.js { render :json => { :results => @events }, :callback => params[:callback] } 确实如此。 (并将 sencha 更改为 url 为 'localhost:3000/search/by_date.js'。
    • 或 format.js { render :js => params[:callback] + "(" + { :results => @events }.to_json + ");"}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多