使用Angular 4解析Django JsonResponse

时间:2017-09-27 10:11:48

标签: python json django angular request

我使用Django发送JsonResponse。如果我映射response.json()Angular返回一个我无法处理的错误。此外,如果我使用response.text()来显示数据,它会返回类似的内容:

  

回复:{u' foo':你' bar',你'标题':你好世界'}

在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

1 个答案:

答案 0 :(得分:0)

from django.http import JsonResponse

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