JQuery ajax 的使用
$.ajax({ # Jquery ajax 对象
url : '/host', #提交地址,相当于form 中的 action
type: 'post', #提交方式,相当于form 中的 method
data: {'k1':'v1','k2','v2'} # 数据
success:function(data){ #接受成功处理方式 ,data为接受成功处理方式
alert(data)
}
})
templates 下的 ajxa.html
function DoAjax(){
var temp=$('#na').val()
$.ajax({
url:'/ajax/', //url相当于 form 中的 action
type:'POST', //type相当于form 中的 method
data:{dat:temp}, // data:传人的数据 dat为任意设置的内容,相当于模版中的{author:lee}
success:function(arg){ //成功执行 console.log() 函数 arg 为HttpResponse 返回的值
console.log('成功')
},
error:function(){ //失败
console.log('失败')
}
});
}
注意这里url的路径为 '/ajax/' ajax 的路径一定要以 '/' 开头
views.py
def ajax(request):
if request.method=='POST':
print(request.POST)
return HttpResponse('ok')
else:
return render(request,'ajax.html')
print(request.POST)
值为ajax 传人的对象