django基础~jquery交互

一 绑定事件

  整体结构 $(selector).click(function(){$.ajax({})})

   1 #selector 为选择器标签ID 记得名字要加#    2 $.ajax({method: http方法 url:"url_context",data:JSON.stringify
{“data”:variables},success:function(arg){},error:function(arg){})
     1 success:function(arg){
         var obj = jQuery.parseJSON(arg); //返回的数据进行join解析
         alert(obj)
        window.location.reload(); //页面刷新
        }}
     2 error:function(arg){
        var obj = jQuery.parseJSON(arg);
       alert(obj)
    }
二 使用案例
<script src="/static/js/jquery.js"></script>
<script src="/static/js/pintuer.js"></script>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script type="text/javascript">
function test(){
alert("11111111")
}
$("#delete_user").click(function() {
var id = $(this).parents("tr").children("td:nth-child(1)").text();
alert("测试")
$.ajax({
url: "/api/v1/",
method: "delete",
data: JSON.stringify({"username":id}),
success: function (arg) {
window.location.reload()
alert("删除成功")
 
},
error: function (arg) {
var obj = jQuery.parseJSON(arg);
alert(obj)
}
})
})
</script>
 

原文地址:https://www.cnblogs.com/danhuangpai/p/14578288.html