Py学习  »  问与答

django返回json数据问题!!

hsdtsyl • 8 年前 • 5006 次点击  

if auth_user:

auth.login(request, auth_user)

authed_user = serializers.serialize('json',
fxcn_user.objects.filter(username=user))

return HttpResponse (json.dumps(authed_user),content_type="application/json")


前端jquery得到的json数据为:

"[{\"fields\": {\"username\": \"figure\", \"first_name\": \"\", \"last_name\": \"\", \"phone\": null, \"updated_at\": \"2015-06-27T02:02:17.622Z\", \"created_at\": \"2015-06-27T02:02:17.622Z\", \"is_active\": true, \"sig_info\": null, \"office_ext\": null, \"sex\": null, \"is_superuser\": true, \"birthday\": null, \"last_login\": \"2015-07-01T08:01:31.844Z\", \"groups\": [], \"user_permissions\": [], \"password\": \"pbkdf2_sha256$20000$GCVkdJZAApJC$qZLsefU0HqKKG9M4Y9OPgI4ZWMUYAIC8tInNIOSBKRo=\", \"friends\": null, \"email\": \"f@f.com\", \"icon\": \"user_icon/default.jpeg\"}, \"model\": \"fxcn_auth.fxcn_user\", \"pk\": 1}]"


前端jquery ajax成功后的处理函数 success:function(response, status, xhr){

$('#login_form').dialog('widget').find('button').eq(1).button('enable')

alert(response);

请问: 如果从返回的json数据中得到用户名,邮箱等信息??

我尝试了alert(response.username)提示undefined; }

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1139
 
5006 次点击  
文章 [ 2 ]  |  最新文章 8 年前
edsion-weibo
Reply   •   1 楼
edsion-weibo    8 年前
from django.http import JsonResponse

不是django的锅

hsdtsyl
Reply   •   2 楼
hsdtsyl    8 年前

先说一声,他大爷的!

/* $.each(response, function(index, item){

$.each(item, function(index, a){

$.each(a, function(index, b){

//alert(b);

});

});

});*/ 竟然用这种鸟方法试验出来的,百度django json那么多页,竟然没一个答案;

写在这里,做个纪念吧!

这样获取用户名就可以了;干!

alert(response[0].fields.username);


最后想说: 我很想在这个论坛发贴,但是管理员能否把这个富文本编辑器给换个,这样发贴实在是很蛋疼,格式一团糟,并不是每个人都会用markdown,比如说我!^_^