2017-09-27 59 views
0

我使用Django發送JsonResponse。如果我映射response.json()Angular返回一個我無法處理的錯誤。此外,如果我用response.text()用於可視化數據就返回類似的東西:解析Django JsonResponse與角4

響應:{u'foo ':u'bar',u'title「:u'hello世界}

在角4我有這樣的代碼:

return this._http.post(this.serverUrl, JSON.stringify(data), {headers: headers}) 
     .map((response:Response) => response.json()) 
     .catch(this.handleError); 

在Django的Python中我有這樣的代碼:

response= {'foo': 'bar', 'title': 'hello world'} 
return JsonResponse(response, safe=False); 

我已經也tryed這一點:

return HttpResponse(json.dumps(response), content_type='application/json; charset=utf-8',) 
return HttpResponse(json.dumps(response)) 
return json.dumps(response) 
return response 
+1

什麼是你真正從服務器上得到的迴應?以「R」開頭的那個。 – hlfrmn

+0

服務器返回給我的這個響應(用Postman測試): 'Response:Content-Type:application/json {「title」:「hello world」} –

+0

'return HttpResponse( json.dumps(responseDictionary), mimetype =「application/json」 )'?另外,你有沒有檢查你是否有一些時髦的中間件轉換東西? – hlfrmn

回答

0
from django.http import JsonResponse 

def profile(request): 
    data = {'foo': 'bar', 'title': 'hello world'} 
    return JsonResponse(data)