最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Django Ajax的使用教程

 更新時(shí)間:2017年06月05日 09:26:30   作者:淺雨涼  
這篇文章主要介紹了Django Ajax的使用教程,需要的朋友可以參考下

簡(jiǎn)介:

AJAX = Asynchronous JavaScript and XML(異步的 JavaScript 和 XML)。

AJAX 不是新的編程語言,而是一種使用現(xiàn)有標(biāo)準(zhǔn)的新方法。

AJAX 是與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁(yè)的藝術(shù),在不重新加載整個(gè)頁(yè)面的情況下。

Ajax

  很多時(shí)候,我們?cè)诰W(wǎng)頁(yè)上請(qǐng)求操作時(shí),不需要刷新頁(yè)面。實(shí)現(xiàn)這種功能的技術(shù)就要Ajax!

jQuery中的ajax就可以實(shí)現(xiàn)不刷新頁(yè)面就能向后臺(tái)請(qǐng)求或提交數(shù)據(jù)的功能,現(xiàn)用它來做django中的ajax,所以先把jquey下載下來,版本越高越好。

一、ajax發(fā)送簡(jiǎn)單數(shù)據(jù)類型:

html代碼:在這里我們僅發(fā)送一個(gè)簡(jiǎn)單的字符串

views.py

 #coding:utf8
 from django.shortcuts import render,HttpResponse,render_to_response
 def Ajax(request):
   if request.method=='POST':
     print request.POST
     return HttpResponse('執(zhí)行成功')
   else:
     return render_to_response('app03/ajax.html')

ajax.html

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Ajax</title>
 </head>
 <body>
   <input id='name' type='text' />
   <input type='button' value='點(diǎn)擊執(zhí)行Ajax請(qǐng)求' onclick='DoAjax()' />
   <script src='/static/jquery/jquery-3.2.1.js'></script>
   <script type='text/javascript'>
   function DoAjax(){
     var temp = $('#name').val();
     $.ajax({
       url:'app03/ajax/',
       type:'POST',
       data:{data:temp},
       success:function(arg){
         console.log(arg);
       },
       error:function(){
         console.log('failed')
       }
     });
   }
   </script>
 </html>

運(yùn)行,結(jié)果:

二、ajax發(fā)送復(fù)雜的數(shù)據(jù)類型:

html代碼:在這里僅發(fā)送一個(gè)列表中包含字典數(shù)據(jù)類型

由于發(fā)送的數(shù)據(jù)類型為列表 字典的格式,我們提前要把它們轉(zhuǎn)換成字符串形式,否則后臺(tái)程序接收到的數(shù)據(jù)格式不是我們想要的類型,所以在ajax傳輸數(shù)據(jù)時(shí)需要JSON

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-">
 <title>Ajax</title>
 </head>
 <body>
   <input id='name' type='text' />
   <input type='button' value='點(diǎn)擊執(zhí)行Ajax請(qǐng)求' onclick='DoAjax()' />
   <script src='/static/jquery/jquery-3.2.1.js'></script>
   <script type='text/javascript'>
   function DoAjax(){
     var temp = $('#name').val();
     $.ajax({
       url:'app03/ajax/',
       type:'POST',
       data:{data:temp},
       success:function(arg){
         var obj=jQuery.parseJSON(arg);
         console.log(obj.status);
         console.log(obj.msg);
         console.log(obj.data);
         $('#name').val(obj.msg);
       },
       error:function(){
         console.log('failed')
       }
     });
   }
   </script>
 </html>

views.py

 #coding:utf
 from django.shortcuts import render,HttpResponse,render_to_response
 import json
 # Create your views here.
 def Ajax(request):
   if request.method=='POST':
     print request.POST
     data = {'status':,'msg':'請(qǐng)求成功','data':['','','']}
     return HttpResponse(json.dumps(data))
   else:
     return render_to_response('app/ajax.html')

打印數(shù)據(jù)樣式:

以上所述是小編給大家介紹的Django Ajax的使用教程,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

田东县| 新昌县| 延吉市| 梅河口市| 仁布县| 皋兰县| 甘谷县| 和政县| 大渡口区| 拉萨市| 万州区| 沧源| 麻阳| 云安县| 饶河县| 类乌齐县| 永顺县| 孝义市| 绿春县| 吴桥县| 汝州市| 黄龙县| 汉阴县| 威远县| 乌恰县| 蛟河市| 抚顺市| 新沂市| 平邑县| 嵊泗县| 昔阳县| 澎湖县| 革吉县| 云霄县| 东光县| 玛曲县| 射阳县| 库尔勒市| 通河县| 枣庄市| 塘沽区|