jQuery Ajax调用asp.net webmethod

时间:2011-08-01 09:49:40

标签: jquery asp.net

任何人都可以说 我们如何在Jquery Ajax调用中指定成功函数。 假设我的函数类似于`function

getComments(data,url,SucessFunction,FailurFunction) {

    var list = [data];
    var jsonData = JSON.stringify({ list: list });

$.ajax({
    type: "POST",
    url: url,
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response, status) {
        var List = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
        $.each(List, function() {
            if (this['Cid'] != "1000")
                $('#' + ddlCities).append('<option value="' + this['Cid'] + '">' + this['CityCode'] + '</option>');
            else ($('#outerDiv').html(this['City']));

        });
    }
});

} `

在此如何定义成功函数,错误函数

喜欢

getComments(data,url,SucessFunction,FailurFunction) {

var list = [data];
var jsonData = JSON.stringify({ list: list });

$.ajax({
    type: "POST",
    url: url,
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: SucessFunction

    }
});

}

提前致谢。

2 个答案:

答案 0 :(得分:0)

仅供您参考:根据您的需要

 AjaxPageMethod("your method", { " ": " " }, ajaxCallSuccess, ajaxCallFailure, "page name");

function ajaxCallSuccess(response) {
    var msg = response.d;
    $("tab").html(msg);
}

function ajaxCallFailure(response) {
    var msg = response.d;
}

函数AjaxPageMethod(fn,reqObject,successFn,errorFn,aspxPage){

    var dataObject = JSON.stringify(reqObject);

    //Call the page method
    $.ajax({
        async: false,
        type: "POST",
        url: aspxPage + "/" + fn,
        contentType: "application/json;",
        data: "{'reqObject':" + dataObject + "}",
        dataType: "json",
        success: successFn,
        error: errorFn
    });
};

答案 1 :(得分:0)

我发现使用Web方法是片状的,您可能想采用稍微不同的方法,而不是使用web方法,看一下使用启用了Ajax的WCF服务?

以下是关于如何设置using wcf to build fast and lean web apps

的博客文章