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

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

问题描述

我有一个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,
纬度:60.0,
位置:null,
updated_at:2011- 04-14T16:38:54Z,
id:1,
longitude:30.0,
took_place_at:2011-03-23T13:32:00Z

{
name:x,
created_at:2011-03-23T13:36:44Z,
纬度:60.0 ,
location:null,
updated_at:2011-03-23T13:36:44Z,
id:2,
longitude:30.0 ,
taken_place_at:2011-03-23T13:36:00Z
},
{
name:Gruempi,
created_at :2011-03-24T14:00:32Z,
纬度:60.0,
location:null,
updated_at:2011-04-14T16:39:47Z,
id:3,
longitude:30.0,
take_place_at:2011-03-24T14:00:00Z
},
{
name:ba,
created_at:2011-04 -10T22:40:07Z,
纬度:60.0,
位置:12,
14,
updated_at:2011-04-10T22 :40:07Z,
id:4,
经度:30.0,
took_place_at:2011-04-10T22:36:00Z
} ,
{
name:sfasdfs,
created_at:2011-04-10T22:44:55Z,
纬度:60.0,
location:我们试图找到你...,
updated_at:2011-04-10T22:44:55Z,
id:5,
经度:30.0,
took_place_at:2011-04-10T22:42:00Z
},
{
name:asdfsad,
created_at:2011-04-10T22:55:03Z,
latitude:60.0,
location:尝试找到你..asfd。,
updated_at:2011-04-10T22:55:03Z,
id:6,
longitude:30.0,
took_place_at:2011-04-10T22:54:00Z
}
]
}

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

 刷新= 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()
},
回调:function(data){
var events = data.results;
timeline.update(events);
}
});
};

和sencha只是不解析它。同时它可以像Twitter一样的API调用。



任何想法我做错了什么?或者我如何找到错误?

解决方案

如果你正在做一个JSONP请求,你将需要将返回的JSON包装在函数被指定为GET请求中的参数,在您的情况下,回调。 Sencha touch将内部处理函数命名,但您需要注意您传入的callbackKey选项,以便您的服务器可以正常响应。



请求 http:// localhost:3000 / search / by_date.json?callback = jsonP2343243243 预期响应应该包装在函数由回调参数指定。该GET请求应该导致以下JSON:



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



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



Rails< 3.0:

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

Rails> 3.0

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


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

The output looks like this:

{
    "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"
        }
    ]
}

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);
    }
  });
};

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?

解决方案

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.

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": [ ... ] });

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:

Rails < 3.0:

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

Rails > 3.0

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

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

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