django通过ajax发起请求返回JSON格式数据的方法

WBOY
Release: 2016-06-10 15:10:48
Original
1348 people have browsed it

本文实例讲述了django通过ajax发起请求返回JSON格式数据的方法。分享给大家供大家参考。具体实现方法如下:

这是后台处理的:

def checkemail(request):
  user = None
  if request.POST.has_key('email'):
    useremail = request.POST['email']
    result = {}
    user = User.objects.filter(useremail__iexact = useremail)
  if user:
    result = "1"
    result = simplejson.dumps(result)
  else:
    result = "0"
    result = simplejson.dumps(result)
  return HttpResponse(result, mimetype='application/javascript')

Copy after login

这是AJAX部分:

if(valid_email($('#reg-for-email').val())){
  var email = $('#reg-for-email').val();
  //这里把用户输入的EMAIL地址提交到后台数据库中去验证是否已存在。
  $.ajax({
    type:"POST" ,
    url:"/reg/checkemail",
    data:"email=" + email ,
    cache: false,
    success: function(result){
  if (result==1)
   {
    $("#reg-for-email-msg").removeClass("g-hide");
    $('#reg-for-email-msg').removeClass("msg-isok").addClass("msg-error").html("该邮箱已存在!");
    eok = true;
   }
   else
   {
    $("#reg-for-email-msg").addClass("g-hide");
    eok = false;
   }
    }
   })
}

Copy after login

URL的配置是:

复制代码 代码如下:
url(r'^reg/checkemail/', 'reg.views.checkemail', name='ce'),

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!