2017-01-03 3 views
-1

私のajaxフォームのフッタセクションにエラーメッセージを表示しようとしていますが、値の代わりに「null」が表示されます。ここ次のコードは、私のajaxフォームの値の代わりに 'null'を表示するのはなぜですか?

null displaying

私のコードは、これまでのところです:

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コード:

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の配列のJavaScriptのと同じですか? –

+0

'data:{ tagId:this.value、 'id':tagId、 } 'これは混乱しませんか?これはあなたがここで考えるものではありません。 – Jai

答えて

0

thisは間違ったコンテキストにある:

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'); 
     } 
    }); 
} 

やAJAXでcontext:this,オプションを使用するには:

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

まだ動作していません:( – Ahtisham

0

論理的に誤っているようです。

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

セカンドライン

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)はrequest.GET.get( 'username'、None)に拡張され、ajaxで 'this.value'によって返される値を取得します??? – Ahtisham

関連する問題