简体   繁体   English

用Angular 4解析Django JsonResponse

[英]Parse Django JsonResponse with Angular 4

I'm using Django for send JsonResponse. 我正在使用Django发送JsonResponse。 If I map response.json() Angular return me an error that I can't handle. 如果我将response.json()映射为Angular,则会返回我无法处理的错误。 Furthermore, if I use response.text() for visualizing data it return something like that: 此外,如果我使用response.text()可视化数据,它将返回如下内容:

Response: {u'foo': u'bar', u'title': u'hello world'} 响应:{u'foo':u'bar',u'title':u'hello world'}

In Angular 4 I've this code: 在Angular 4中,我有以下代码:

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

In Django Python I've this code: 在Django Python中,我有以下代码:

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

I've also tryed this: 我也尝试过这个:

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