将数据从django服务视图发送到java脚本(chrome扩展)

时间:2018-02-21 05:58:29

标签: python django google-chrome

我正在从chrome扩展程序向django服务发送电子邮件和密码,以检查数据库中是否存在电子邮件和密码。接下来,我必须将django服务的响应发送到java脚本(chrome扩展)。

Chrome扩展程序中的javascript:

document.addEventListener('DOMContentLoaded', loginEvents, false);

function myAction(femail,fpassword) {
    //alert("femail=" + femail.value + "fpassword=" +fpassword.value);
    var strLogin = "email=" + femail.value + "&password=" + fpassword.value;
    if (femail.value == ""){
        alert("Username must be filled out");
        return false;
    }
    if (fpassword.value == ""){
        alert("Password must be filled out");
        return false;
    }
    var newxmlhttp = new XMLHttpRequest();
    var theUrl = "http://127.0.0.1:8000/polls/login/?";
    newxmlhttp.open("POST", theUrl, true);

    newxmlhttp.onreadystatechange = function() {
        if (newxmlhttp.readyState == 4){
            alert("entered");
        }
        else{
            alert("not entered");
        }

    };
    newxmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
    newxmlhttp.send(strLogin);
}
function loginEvents() {
    console.log("entered console");
    var loginSubmitButton = document.getElementById('loginSubmit')
    loginSubmitButton.addEventListener('click', 
    function(event) {
        var userEmail = document.getElementById('email');
        var userPassword = document.getElementById('password');
        myAction(userEmail,userPassword);
    });
}

views.py:

from django.http import HttpResponse
from django.http import HttpResponseRedirect, HttpResponse
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render
from .models import UserProfile
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def login(request):
  print(request.method)
  useremail = request.POST.get('email')
  userpassword = request.POST.get('password')
  print('email %s %s' % (useremail,userpassword))
  try:
    /* to check the data is present in database or not */ 
    entry = UserProfile.objects.get(email=useremail,password=userpassword)
    print('matched== %s %s' %(useremail,userpassword))
    return HttpResponse('hello %s' % useremail)
  except ObjectDoesNotExist:
    print('email not matched')

我能够从Django获取已从chrome扩展发送的数据,但我无法理解如何将数据从Django发送到chrome扩展。我理解如何获取由Django服务发送的chrome扩展中的数据,但我无法理解如何从Django发送数据。

1 个答案:

答案 0 :(得分:0)

您可以在响应处理程序中获取服务器响应,如下所示:

    newxmlhttp.onreadystatechange = function() {
        if (newxmlhttp.readyState == 4){
            // Alert response
            alert(newxmlhttp.responseText);
        }
    };

<强>更新

将数据添加到响应中,我认为使用JSON数据将非常容易:

from django.http import HttpResponse, JsonResponse
from django.http import HttpResponseRedirect, HttpResponse
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render
from .models import UserProfile
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def login(request):
  print(request.method)
  useremail = request.POST.get('email')
  userpassword = request.POST.get('password')
  print('email %s %s' % (useremail,userpassword))
  try:
    /* to check the data is present in database or not */ 
    entry = UserProfile.objects.get(email=useremail,password=userpassword)
    print('matched== %s %s' %(useremail,userpassword))
    return JsonResponse({"status": "ok", "message": 'hello %s' % useremail}, status=200)
  except ObjectDoesNotExist:
    return JsonResponse({"status": "fail", "message": 'email not matched'}, status=400)