2017-01-03 2 views
-1

Ich versuche, die Fehlermeldung im Fußbereich meiner Ajax-Form anzuzeigen, aber ich bekomme 'Null' anstelle eines Wertes, was ist falsch?Warum zeigt der folgende Code "Null" anstelle eines Werts in meiner Ajax-Form an?

null displaying

hier ist mein Code so weit:

html:

<div class="modal-footer"> 
    <div id="error_message"></div> 
    <button type="submit" class="btn btn-default" id="submit" disabled>Submit</button> 
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
</div role="modal-footer"> 

view.py:

from django.shortcuts import render 
from librarysystem.models import Users 
from django.http import JsonResponse 

def index(request): 
    template = 'librarysystem/Elib.html' 
    return render(request,template) 

    def validateForm(request): 
     tagId = request.GET.get('id',None)   
     data = { 
       'isTaken' : tagId, 
       'value': request.GET.get(tagId,None), 
       } 
     return JsonResponse(data) 

AJAX-Code:

function validateForm() { 
    tagId = this.id; 
    $.ajax({ 
     url: "/librarysystem/validate/", 
     data: { 
       tagId: this.value, 
       'id': tagId, 
     }, 
     dataType: 'json', 
     success: function(data){ 
      $('#error_message').html(data.value + ' = value').css('color','red'); 
     } 
    }) ; 
} 

$(document).ready(function() {           
    $("#username, #emailid, #password, #retrypassword").keyup(validateForm); 
}); 
+0

Python-Array sind die gleichen wie Javascripts? –

+0

'daten: { tagId: this.value, 'id': tagId, },' ist das nicht verwirrend? obwohl 'das' nicht das ist, was du hier denkst. – Jai

Antwort

0

this ist in falschem Kontext:

function validateForm() { 
     tagId = this.id; 
    var val = this.value; // <-----get value here 
    $.ajax({ 
     url: "/librarysystem/validate/", 
     data: { 
       tagId: val, //<----pass it here 
       'id': tagId 
     }, 
     dataType: 'json', 
     success: function(data){ 
      $('#error_message').html(data.value + ' = value').css('color','red'); 
     } 
    }); 
} 

oder context:this, Option in der Ajax verwenden:

function validateForm() { 
     tagId = this.id; 

    $.ajax({ 
     url: "/librarysystem/validate/", 
     context:this, //<----add it here 
     data: { 
       tagId: this.value, 
       'id': tagId 
     }, 
     dataType: 'json', 
     success: function(data){ 
      $('#error_message').html(data.value + ' = value').css('color','red'); 
     } 
    }); 
} 
+0

funktioniert immer noch nicht :( – Ahtisham

0

Es scheint logisch falsch.

tagId = request.GET.get('id',None) ## Which returns username based on the request 

Zweite Linie

data = { 
     'isTaken' : tagId, 
     'value': request.GET.get(tagId,None) ## request params doesn't have any key as "**username**" and is supposed to return null. 
} 
+0

request.GET.get (tagId, None) wird auf request.GET.get ('username', None) erweitert, was den Wert von 'this.value' in ajax ??? – Ahtisham

Verwandte Themen