Sencha 似乎不喜欢 Rails 的 json [英] Sencha seems to not like Rails' json

查看:14
本文介绍了Sencha 似乎不喜欢 Rails 的 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a Rails controller which is rendering some models to 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 使用该服务:

and I try to consume the service with 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 调用配合使用.

and sencha just doen't parse it. Meanwhile it works with an API-call like Twitter.

知道我做错了什么吗?或者我如何才能找到错误?

Any ideas what I've done wrong? Or how I can find the bug?

推荐答案

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

If you are doing a JSONP request you will need to wrap the returned JSON in the function that is specified as a parameter in the GET request, in your case 'callback'. Sencha touch will handle the function naming internally but you do need to take note of the callbackKey option you pass in so that your server can respond properly.

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

When requesting http://localhost:3000/search/by_date.json?callback=jsonP2343243243 the expected response should be wrapped in a function that is specified by the callback parameter. That GET request should result in the following JSON:

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

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

This will cause that function to be called when it is interpreted by the browser and then the callback of the AJAX will be called. In rails you would need to change your rendering to something like:

导轨<3.0:

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

Rails > 3.0

Rails > 3.0

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

这篇关于Sencha 似乎不喜欢 Rails 的 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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