C#之委託

直接上代碼

javascript:javascript

function get(url, success) {
    $.ajax({
        type: "Get",
        url: url,
        data: {},
        contentType: "application/json",
        dataType: "json",
        success: success || function (res) { },
        error: function () {
            alert("ajax error");
        }
    });
}

//調用
get("/api/sample", function (res) {
    console.log(res);
});

//使用箭頭函數簡化
get("/api/sample", (res) => {
    console.log(res);
});

C#java

public static void Get(string url, Action<string> success)
{
    WebClient client = new WebClient();
    string result = client.DownloadString(url);
    success(result);
}

//調用
Get("/api/sample", delegate (string res)
{
    Console.WriteLine(res);
});

//使用lambda表達式簡化
Get("/api/sample", (res)=>{
    Console.WriteLine(res);
});

結論

還要說總結什麼嗎?ajax

相關文章
相關標籤/搜索