我试图在我的ajax表单的页脚部分显示错误消息,但是我得到'null'而不是值出错了什么?
到目前为止,这是我的代码:
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 :(得分: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');
}
});
}
答案 1 :(得分: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.
}