1. 程式人生 > >前端Ajax請求、HTTP POST請求、HTTP GET請求示例

前端Ajax請求、HTTP POST請求、HTTP GET請求示例

Ajax請求

示例一

function check() { //定義變數sendData var sendData ={id:“123456”,status:“222”}; $.ajax({ url:basePath + ‘/getUser’, type:‘post’, dataType:‘json’, contentType:“application/json”, async:true,//非同步請求 cache:false, data:JSON.stringify(sendData),//使用變數sendData //執行成功的回撥函式 success:function(data) { alert(data.info

) }, //執行失敗或錯誤的回撥函式 error:function(data) { alert(“認證失敗”); } }); }123456789101112131415161718192021

示例二

Handler中用【@RequestParam Map<String, String> map】接收前段資料

確認刪除?’ ,btn: [ ‘確認’, ‘取消’] ,btnAlign: ‘c’ //按鈕居中 ,shade: 0.3 //不顯示遮罩 ,yes: function(){ //定義變數sendData var sendData ={id:grid.id,user:grid.createUser}; $.ajax({ data: sendData, url:“account/delAccount”, dataType: “json”, type: ‘POST’, success:function(json) { layer.msg(json.msg); $(’#table’).bootstrapTable(‘refresh’, {url:‘account/query’}); } }); layer.closeAll(); } ,btn2: function(index, layero){ layer.closeAll(); } });123456789101112131415161718192021222324252627

示例三

$.ajax({ url: url, data: $(’#dirForm’).serialize(), dataType: “html”, type: “post”, success: function(result) { $(’#jumpbox’).html(result); $(’#jumpbox’).modal(‘show’); } });12345678910

示例四

(function((function() { $(".reset").click(function() { $("#search_form :text").val(""); $("#search_form ?eq(0)").prop(“checked”, “checked”); $("#search_form select option:eq(0)").prop(“selected”, “selected”); });

$(".delete").click(function() {
    var userId = $(this).data("userid");
    $('#sure_msg_body').html('確定要刪除嗎?此操作不可逆,請謹慎選擇!');
    $('#sure_msgbox').modal('show');
    $("#sure_btn").unbind("click");
    $("#sure_btn").click(function() {
        $.ajax({
            url: "delete",
            data: {
                userId: userId
            },
            dataType: "json",
            type: "post",
            success: function(data) {
                if (data.status == "1") {
                    $('#msg_body').html('刪除成功');
                    $('#msgbox').modal('show');
                    $("#msg_btn").click(function() {
                        window.location.href = "${stcds}/userManager/query";
                    });
                } else {
                    if (typeof data.info == "object") {
                        $('#msg_body').html(data.info[0].defaultMessage);
                        $('#msgbox').modal('show');
                    } else {
                        $('#msg_body').html(data.info);
                        $('#msgbox').modal('show');
                    }
                }
            }
        });
    });

});

});12345678910111213141516171819202122232425262728293031323334353637383940414243

POST請求

示例一

.post(&quot;.post(&quot;{stcds}/dir/" + dirId + “/delete.msg”, function(result) { $(’#jumpbox’).html(result); $(’#jumpbox’).modal(‘show’); });1234

示例二

KaTeX parse error: Expected '}', got 'EOF' at end of input: …ction(){ txt=(“input”).val(); $.post(“demo_ajax_gethint.asp”,{suggest:txt},function(result){ $(“span”).html(result); }); });123456

JS常用操作

function clearValue(formId){ $("#" +formId+ " input:text").val(’’); $("#" +formId+ " input:radio").attr(‘checked’, false); $("#" +formId+ " input:checkbox").attr(‘checked’, false); $("#" +formId+ " select").val(’’); $("#dirCode").val(’’); }