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",
"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 调用配合使用。
有什么想法我做错了什么吗?或者我怎样才能找到错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在执行 JSONP 请求,则需要将返回的 JSON 包装在 GET 请求中指定为参数的函数中,在您的例子中为“回调”。 Sencha touch 将在内部处理函数命名,但您确实需要记下传入的callbackKey 选项,以便您的服务器可以正确响应。
请求 http://localhost:3000/search/by_date.json?callback=jsonP2343243243 预期响应时应包装在回调参数指定的函数中。该 GET 请求应生成以下 JSON:
jsonP2343243243({ "results": [ ... ] });
这将导致该函数在被浏览器解释时被调用,然后AJAX 的回调将被调用。在 Rails 中,您需要将渲染更改为:
Rails < 3.0:
轨道> 3.0
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:
Rails > 3.0