以下ajax代码有什么问题? [英] What is wrong with the following ajax code?
问题描述
我正在尝试将this对象的id从ajax调用传递给django视图,但是在我的博客中收到以下错误:
因为我是初学者django和ajax,这就是为什么代码中可能有错误。
这是我的代码:
views.py
$来自django.shortcuts的$ b从django.http导入JsonResponse
def index(request):
template ='librarysystem / Elib.html'
return render(request,template)
def validateForm(request,id):
data = {
'isTaken':Users.objects.filter(username__iexact = request.GET.get(id,None))。exists(),
#'emailidTaken':Users.objects .filter(emailid__iexact = emailid).exists(),
}
返回JsonResponse(数据)
urls.py
from django.conf.urls import url
从.import视图
urlpatterns = [
url(r'^ registered / $',views.create_user),
url(r'^ $',views.index ,name =Index),
url(r'^ validate(?P< id>)/ $',views.validateForm),
#url(r'^ article / $',views.Article,name =Article),
]
ajax代码
< script type =text / javascript >
函数validateForm(){
// var emailid = $('#emailid')。val();
$ .ajax({
url:'/librarysystem/validate/this.id/',
data:{
'this.id':this.value,
//'emailid':emailid,
},
dataType:'json',
success:function(data){
if(data.isTaken){
$('#error')。html(this.id +'已经被占用')css('color','red');
} else
$('#error' ).html('');
}
});
}
$(document).ready(function(){
$('#retrypassword')。keyup(checkPasswordMatch);
$(#username,#emailid, #password,#retrypassword)。keyup(validateForm);
});
< / script>
感谢您的回复:)
附件屏幕截图给出了一个明确的答案,找不到请求的URL(错误404)。正如我在上面的评论中提到的,这是因为您使用AJAY网址的简单字符串:'/ librarysystem / validate / this.id /'
而不是连接静态和url的动态部分:
$。ajax({
url:'/ librarysystem / validate /'+ this .value +'/',
//其余的代码....
data:{}
});
但是从代码示例中我看到的可能不是唯一的问题 - 在给定的代码没有提到' this.value
。
I am trying to pass the id of 'this' object from ajax call to django view but i am getting the following error in my broswer :
as i am beginner to both django and ajax that is why there could be errors in the code.
here is my code :
views.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, id):
data = {
'isTaken' : Users.objects.filter(username__iexact=request.GET.get(id,None)).exists(),
#'emailidTaken' : Users.objects.filter(emailid__iexact=emailid).exists(),
}
return JsonResponse(data)
urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^registered/$',views.create_user),
url(r'^$', views.index, name="Index"),
url(r'^validate(?P<id>)/$',views.validateForm),
#url(r'^article/$', views.Article, name="Article"),
]
ajax code:
<script type="text/javascript">
function validateForm() {
//var emailid = $('#emailid').val() ;
$.ajax({
url: '/librarysystem/validate/this.id/',
data: {
'this.id': this.value,
//'emailid': emailid,
},
dataType: 'json',
success: function(data){
if (data.isTaken){
$('#error').html(this.id + ' Already taken.').css('color','red');
}else
$('#error').html('');
}
}) ;
}
$(document).ready(function() {
$('#retrypassword').keyup(checkPasswordMatch);
$("#username, #emailid, #password, #retrypassword").keyup(validateForm);
});
</script>
thanks for your kind replies :)
The attached screenshot gives a clear answer, the requested URL is not found (error 404). As I mentioned in my comment above this is because you use a simple string for the AJAY URL: '/librarysystem/validate/this.id/'
instead of concatenate the static and dynamic part of the url:
$.ajax({
url: '/librarysystem/validate/' + this.value + '/',
// the rest of your code....
data: {}
});
But from what I see in your code example this might not be the only problem - in your given code there is no reference to 'this.value
.
这篇关于以下ajax代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!