Ajax(jquery)

在js+Ajax中,要区别版本信息,在jquery中不需要。

1.$.ajax()

###xxx.html###  
$.ajax({ url:"/xx/", #url:"//",url最好是这种形式,不然在post请求中会报错500 data:{a:1,b:2}, type:"POST",
        processDate:false #data数据不进行转码(默认是true,是进行转码的)
   traditional:true #
如果data数据中还有其他迭代:data:{a:1,b:[3,4]},传给服务端的数据为{'a':['1'],'b[]':['3','4']},traditional:true是改变这种情况,{'a':['1'],'b':['3','4']

            success:function(data){       #回调函数:当某个动作完成后会自动触发的一个函数(在这里是等浏览器服务端交互完成后接受服务端发来的信息)
                console.log(data);
            }
        });

###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")


$.get(url, [data], [callback], [type])
$.post(url, [data], [callback], [type])  //type: text|html|json|script   type指定什么类型,如果接受到服务端的数据类型不对,那么回调函数不执行

2.$.get()

        $.get("/xx/",function(data){           //data是服务端Httpresponse返回的对象,此为回调函数
           console.log(data)
        });

3.$.post()

        $.post("/xx/",{name:"高玉坤"},function(data,contentType,jqy){      //参数四type:用来查看返回的是否是指定类型,如果不是则不执行回调函数
           console.log(arguments);
            console.log(data);             //高玉坤
            console.log(contentType);      //success/err
            console.log(jqy);              //
        });

4.$.getJSON()

$.get()的最后一个type参数必须是json类型

5.$.getScript()     #引用另一个js文件中的某种方法

$.getScript("/xx.js/",function(){
   alert(add(1,3)) ;
})

###xx.js###
function add(x,y){
  return x+y;  
}

猜你喜欢

转载自www.cnblogs.com/gaoyukun/p/9037710.html