Ajax(jquery)
生活随笔
收集整理的這篇文章主要介紹了
Ajax(jquery)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在js+Ajax中,要區(qū)別版本信息,在jquery中不需要。
Ajax用jquery實現(xiàn)主要有三個API:$.ajax()/$.get()/$.post()
1.$.ajax()
###xxx.html###$.ajax({url:"/xx/", #url:"//",url最好是這種形式,不然在post請求中會報錯500data:{a:1,b:2},type:"POST",
processDate:false #data數(shù)據(jù)不進行轉碼(默認是true,是進行轉碼的)
traditional:true #如果data數(shù)據(jù)中還有其他迭代:data:{a:1,b:[3,4]},傳給服務端的數(shù)據(jù)為{'a':['1'],'b[]':['3','4']},traditional:true是改變這種情況,{'a':['1'],'b':['3','4'] success:function(data){ #回調(diào)函數(shù):當某個動作完成后會自動觸發(fā)的一個函數(shù)(在這里是等瀏覽器服務端交互完成后接受服務端發(fā)來的信息)console.log(data);
console.log(arguments); #d打印函數(shù)的參數(shù)
} });
###views.py###
def xxx(req):
return render(req,"xxx.html")
def xx(req):
if req.method=="POST":
name=req.POST.get("a") #從瀏覽器接受到的a值為1
return HttpResponse(name)
else:
return HttpResponse("ooook")
<form id="f1">
? ? ?<input type="text" name="username"/>
? ? ?<input type="password" name="password" />
? ? ?<select mutiple>
? ? ?</select>
</form>
$.ajax({
? ? ?url:....
? ? ?data:$("#f1").seralize() //發(fā)送所有能提交的數(shù)據(jù)
? ? ?traditional:true //如果是多選框,要加上這句
})
?
$.get(url, [data], [callback], [type]) $.post(url, [data], [callback], [type]) //type: text|html|json|script type指定什么類型,如果接受到服務端的數(shù)據(jù)類型不對,那么回調(diào)函數(shù)不執(zhí)行2.$.get()
?
$.get("/xx/",function(data){ //data是服務端Httpresponse返回的對象,此為回調(diào)函數(shù)console.log(data)});3.$.post()
$.post("/xx/",{name:"高玉坤"},function(data,contentType,jqy){ //參數(shù)四type:用來查看返回的是否是指定類型,如果不是則不執(zhí)行回調(diào)函數(shù)console.log(arguments);console.log(data); //高玉坤console.log(contentType); //success/errconsole.log(jqy); //});4.$.getJSON()
$.get()的最后一個type參數(shù)必須是json類型5.$.getScript()? ? ?#引用另一個js文件中的某種方法
$.getScript("/xx.js/",function(){alert(add(1,3)) ; })###xx.js### function add(x,y){return x+y; }?
轉載于:https://www.cnblogs.com/gaoyukun/p/9037710.html
總結
以上是生活随笔為你收集整理的Ajax(jquery)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。