Ajax請求樣本

來源:互聯網
上載者:User

標籤:root   url   刪除   get   port   imp   move   list   table   

模板

 1                 {% for row in host_list %} 2                 <tr> 3                     <td class="c1">{{ row.id }}</td> 4                     <td>{{ row.servername }}</td> 5                     <td>{{ row.serverip }}</td> 6                     <td>{{ row.serverpassword }}</td> 7                     <td>編輯</td> 8                     <td class="i1">刪除</td> 9                 </tr>10                 {% endfor %}11                 </table>12         </div>13     </div>14 15 <script src="/static/jquery-3.1.1.js"></script>16 <script>17     $(".i1").each(function () {18         $(this).click(function () {19             var b = $(this).parent().find("td").get(0).innerText;20             console.log(b);21             $(this).parent().remove();22             $.ajax({23                 url:‘/host-ajax‘,24                 type:"POST",25                 data:{"id":b},26                 success:function (data) {27                     var obj = JSON.parse(data);28                     alert(obj.data)29                 }30             })31         })32     })33 </script>

路由系統

1 url(r‘^host-ajax‘, views.host_ajax),

視圖函數

 1 def host_ajax(request): 2     import  json 3     res = {"status":True,"error":None,"data":None} 4     try: 5         if request.method =="POST": 6             print("執行 post方法 》》") 7             nid = request.POST.get("id") 8             print("nid:>>>",nid) 9             models.HostInfo.objects.filter(id=nid).delete()10             res["data"] = "delete success"11         else:12             pass13     except Exception as e:14         res["status"]= False15         res["error"] = "rquest error"16     return HttpResponse(json.dumps(res))

Ajax 

 

            $.ajax({
            url: ‘/host‘,                                                #要提交到那個URL,觸發那個函數
            type: "POST",                                             #提交方式
            data: {‘k1‘: 123,‘k2‘: "root"},                      #提交的資料
            success: function(data){                             #回呼函數,即只有伺服器端返回資訊以後會自動執行,必要帶一個參數,這個參數代表格服務端返回的字典
                // data是伺服器端返回的字串
                var obj = JSON.parse(data);                    #把伺服器端返回的字串變成一個json字典的格式對象
            }
        })
        
    
        建議:永遠讓伺服器端返回一個字典
        
        return HttpResponse(json.dumps(字典))

 

Ajax請求樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.