簡體   English   中英

用Angular 4解析Django JsonResponse

[英]Parse Django JsonResponse with Angular 4

我正在使用Django發送JsonResponse。 如果我將response.json()映射為Angular,則會返回我無法處理的錯誤。 此外,如果我使用response.text()可視化數據,它將返回如下內容:

響應:{u'foo':u'bar',u'title':u'hello world'}

在Angular 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);

我也嘗試過這個:

return HttpResponse(json.dumps(response), content_type='application/json; charset=utf-8',)
return HttpResponse(json.dumps(response))
return json.dumps(response)
return response
from django.http import JsonResponse

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM