Ajax異步請求的各種使用情況

首先介紹下Ajax的各項參數:

url : 發送請求的地址  (默認: 當前頁地址)

data :發送到服務器的數據 (必須爲 Key/Value 格式)

type : 請求方式 ("POST" 或 "GET"), 默認爲 "GET"。

dataType :

預期服務器返回的數據類型。如果不指定,jQuery 將自動根據 HTTP 包 MIME 信息來智能判斷,比如XML MIME類型就被識別爲XML。在1.4中,JSON就會生成一個JavaScript對象,而script則會執行這個腳本。隨後服務器端返回的數據會根據這個值解析後,傳遞給回調函數。可用值:

"xml": 返回 XML 文檔,可用 jQuery 處理。
"html": 返回純文本 HTML 信息;包含的script標籤會在插入dom時執行。
"script": 返回純文本 JavaScript 代碼。不會自動緩存結果。除非設置了"cache"參數。'''注意:'''在遠程請求時(不在同一個域下),所有POST請求都將轉爲GET請求。(因爲將使用DOM的script標籤來加載)
"json": 返回 JSON 數據 。(推薦使用)
"jsonp": JSONP 格式。使用 JSONP 形式調用函數時,如 "myurl?callback=?" jQuery 將自動替換 ? 爲正確的函數名,以執行回調函數。

"text": 返回純文本字符串

succes :請求成功後的回調函數



1  $.ajax()的方式(推薦使用)


$(".del").click(function(){
        var vedio_this = $(this).closest("tr");
        var vedio_id = $(this).closest("tr").attr("id");
        if(confirm("刪除後將無法恢復,您確定刪除?")){
            $.ajax({
                url:$("#url").val()+"/del_vedio", // 發送請求的地址  (默認: 當前頁地址)
                data:{  //發送到服務器的數據
                    vedio_id:vedio_id
                },
                type:"POST",    //請求方式,後臺服務器獲取的方式爲$_POST["vedio_id"],注:默認爲GET
                dataType:"json",  //返回的數據類型爲json
                success:function(data){  //請求成功後的回調函數
                    if(data.code=="a"){
                        vedio_this.remove();
                        alert("刪除成功");
                    }else{
                        alert("刪除失敗");
                    }
                }
            });
        }

    });


請求成功後的返回函數也可以$.ajax().done(function(){})來代替:

$(".del").click(function(){
        var vedio_this = $(this).closest("tr");
        var vedio_id = $(this).closest("tr").attr("id");
        if(confirm("刪除後將無法恢復,您確定刪除?")){
            $.ajax({
                url:$("#url").val()+"/del_vedio", // 發送請求的地址  (默認: 當前頁地址)
                data:{  //發送到服務器的數據
                    vedio_id:vedio_id
                },
                type:"POST",    //請求方式,後臺服務器獲取的方式爲$_POST["vedio_id"],注:默認爲GET
                dataType:"json"  //返回的數據類型爲json
            }).done(function(data){  //請求成功後的回調函數
                if(data.code=="a"){
                    vedio_this.remove();
                    alert("刪除成功");
                }else{
                    alert("刪除失敗");
                }
            });
        }
    });

2  $.post(url,[data],[callback],[datatype])方式  這是一個簡單的 POST 請求功能以取代複雜 $.ajax 。請求成功時可調用回調函數。如果需要在出錯時執行函數,請使用 $.ajax。

注:datatype如果不寫的話默認是txet

$(".del").click(function(){
        var vedio_this = $(this).closest("tr");
        var vedio_id = $(this).closest("tr").attr("id");
        if(confirm("刪除後將無法恢復,您確定刪除?")){
            $.post(
                $("#url").val()+"/del_vedio",  //url
                {vedio_id:vedio_id},    //data
                function(data){  //請求成功後的回調函數
                    if(data.code=="a"){
                        vedio_this.remove();
                        alert("刪除成功");
                    }else{
                        alert("刪除失敗");
                    }
                },
                "json"  //dataType
            );
        }
    });

3  $.get(url,[data],[callback],[datatype])方式,,這是一個簡單的 GET 請求功能以取代複雜 $.ajax 。請求成功時可調用回調函數。如果需要在出錯時執行函數,請使用 $.ajax。

注意:後臺接收數據時,要用$_GET[" "]接收

$(".del").click(function(){
        var vedio_this = $(this).closest("tr");
        var vedio_id = $(this).closest("tr").attr("id");
        if(confirm("刪除後將無法恢復,您確定刪除?")){
            $.get(
                $("#url").val()+"/del_vedio",  //url
                {vedio_id:vedio_id},    //data
                function(data){  //請求成功後的回調函數
                    if(data.code=="a"){
                        vedio_this.remove();
                        alert("刪除成功");
                    }else{
                        alert("刪除失敗");
                    }
                },
                "json"  //dataType
            );
        }
    });

4   $.getJSON(url,[data],[callback])方式    注:data可選

5   $.getScript(url,[callback])方式   注:callback可選        通過 HTTP GET 請求載入並執行一個 JavaScript 文件。

$.getScript("test.js", function(){
  alert("Script loaded and executed.");
});

6   $.load(url,[data],[callback])方式    注:data和callback可選    載入遠程 HTML 文件代碼並插入至 DOM 中

     如:

$("#feeds").load("feeds.html");

    再如:

 $("#feeds").load("feeds.php", {limit: 25}, function(){
   alert("The last 25 entries in the feed have been loaded");
 });



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章