2013-03-19 50 views
0

在我views.py地方沒有文字,我有Django的響應

def loadFcs(request): 
    r = requests.get('a url') 
    res = json.loads(r.text) 
    #Do other stuff 
    return HttpResponse('some response') 

現在,當我把這個從我的javascript,loadFcs被調用,並可能 requests.get被異步調用。所以,我最終和跟蹤指向符合

res = json.loads(r.text) 

我還修改了代碼,查什麼問題,

def loadFcs(request): 
    r = requests.get('a url') 
    res = json.loads(r.text) 
    if r == None: 
    print 'r is none' 
    if r.text == None: 
    print 'text is None' 
    #Do other stuff 
    return HttpResponse('some response') 

,看到「在/ loadFcs預期字符串或緩衝區類型錯誤」注意到'文字是無'。所以,我想我需要讓是request.GET中同步調整代碼。我認爲,該方法會繼續執行,return語句被擊中r.text有一定的價值,甚至之前。 建議?

+0

你嘗試傾銷'r'看你得到了什麼? – romainberger 2013-03-19 22:33:06

+0

http://stackoverflow.com/questions/13197854/python-requests-fetching-the-head-of-the-response-content-without-consuming-it – catherine 2013-03-20 02:49:12

+0

@romainberger你這是什麼意思傾倒?我已經在檢查r.text是否爲NONE – kk1957 2013-03-20 15:11:34

回答

0

好了,所以我嘗試使用python命令行同樣的事情,它的工作,但不能在我的服務器相同的代碼。 那麼究竟是什麼問題呢?

顯然,response.text在一些編碼(UTF8),它在我的服務器沒有設置接收,所以它只是把它扔了,因此空。

解決方法:使用response.content(這是原始二進制)