這篇文章給大家介紹如何在Django中使用Ajax,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
在灤南等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需求定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,灤南網(wǎng)站建設(shè)費用合理。一、ajax發(fā)送簡單數(shù)據(jù)類型:
html代碼:在這里我們僅發(fā)送一個簡單的字符串
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='點擊執(zhí)行Ajax請求' 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>
運行,結(jié)果:
二、ajax發(fā)送復(fù)雜的數(shù)據(jù)類型:
html代碼:在這里僅發(fā)送一個列表中包含字典數(shù)據(jù)類型
由于發(fā)送的數(shù)據(jù)類型為列表 字典的格式,我們提前要把它們轉(zhuǎn)換成字符串形式,否則后臺程序接收到的數(shù)據(jù)格式不是我們想要的類型,所以在ajax傳輸數(shù)據(jù)時需要JSON
<!DOCTYPE html> <html> <head> <meta charset="UTF-"> <title>Ajax</title> </head> <body> <input id='name' type='text' /> <input type='button' value='點擊執(zhí)行Ajax請求' 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':'請求成功','data':['','','']} return HttpResponse(json.dumps(data)) else: return render_to_response('app/ajax.html')
打印數(shù)據(jù)樣式:
關(guān)于如何在Django中使用Ajax就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享文章:如何在Django中使用Ajax-創(chuàng)新互聯(lián)
分享URL:http://aaarwkj.com/article0/jceoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、ChatGPT、Google、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站制作、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容