2011-01-10 22 views
0

以下是客戶端代碼。它運行insdide一個谷歌小工具使用Google App Engine獲取遠程內容的問題

var params = {}; 
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON; 
    var url = "http://invplatformtest.appspot.com/getrecent/"; 
    gadgets.io.makeRequest(url, response, params); 

的響應函數是:

function response(obj) 
    { 
    var r = obj.data; 
    alert(r['name']); 
    } 

而在服務器端,在Python代碼發送JSON是:

class GetRecent(webapp.RequestHandler): 
    def get(self): 
     self.response.out.write({'name':'geocities'}) #i know this is where the problem is so how do i encode json in GAE? 

這僅僅是應該發回一個JSON編碼字符串 ,但是當我運行這個,JavaScript會引發以下錯誤:

r is null 
    alert(r['name']); 

如果我收到的只是TEXT內容和我的服務器發送文本一切正常。當我試圖發送JSON時,我只會遇到這個問題。問題究竟在哪裏?我編碼的JSON在AppEngine上的錯誤方式?我試過使用JSON庫,但它看起來好像不支持。

準確的問題在哪裏? :(

回答

1

問題是你根本沒有編碼JSON - 你只是把一個Python字典傳給self.request.out.write,期待它知道該怎麼做。導入django.utils.simplejson,並在字典上調用simplejson.dumps()將其轉換爲JSON。

+0

true ...嘗試pythons原生json庫,但顯然這並沒有工作...感謝指出django的simplejson – dade 2011-01-11 10:41:44