2011-04-14 46 views
1

我有一個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" 
     } 
    ] 
} 

,我嘗試用煎茶/ 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); 
    } 
    }); 
}; 

和煎茶只是doen't解析它。同時它可以與Twitter之類的API調用一起使用。

任何想法我做了什麼錯?或者我如何找到錯誤?

回答

7

如果你正在做一個JSONP要求你將需要包裝返回的JSON在被指定爲GET請求中的參數的函數,你的情況「回調」。 Sencha touch將在內部處理函數命名,但您需要注意您傳入的callbackKey選項,以便服務器可以正確響應。

當請求http://localhost:3000/search/by_date.json?callback=jsonP2343243243預期響應應該被封裝在由回調參數指定的功能。這GET請求應該導致以下JSON:

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

這將導致調用該函數時,它是由瀏覽器解釋,然後AJAX的回調將被調用。在軌道,你會需要你的渲染更改爲類似:

Rails的< 3.0:

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

滑軌> 3.0

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

'渲染:JSON => {:結果=> @events },:callback => params [:callback]'它內置於rails 3 – 2011-04-14 18:15:21

+0

謝謝。 format.js {渲染:JSON => {:結果=> @events}:回調=> PARAMS [:回調]} 它是。 (而變化煎茶到URL來的 'http://本地主機:3000 /搜索/ by_date.js' – Beffa 2011-04-14 18:34:23

+0

或format.js {渲染:JS => PARAMS [:回調] + 「(」 + {:結果=> @ events} .to_json +「);」} – Beffa 2011-04-14 18:35:55