简体   繁体   English

jQuery Ajax调用asp.net网络方法

[英]jQuery Ajax Call to asp.net webmethod

Can any one say How we can specify a Success Function in Jquery Ajax call. 谁能说出如何在Jquery Ajax调用中指定成功函数。 Suppose I have function like below `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']));

        });
    }
});

} ` }`

In this how can define the success function ,error function 在此如何定义成功函数,错误函数

Like 喜欢

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

    }
});

} }

Thanks in advance. 提前致谢。

just for your ref: as per your needs 仅供您参考:根据您的需求

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

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

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

function AjaxPageMethod(fn, reqObject, successFn, errorFn, aspxPage) { 函数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
    });
};

I have found using Web Method to be flaky, your might want to take a slightly different approach, instead of using a webmethod, take a look at using ajax enabled WCF service? 我发现使用Web方法很不稳定,您可能想采用稍微不同的方法,而不是使用Web方法,而是使用启用了Ajax的WCF服务?

Here is a blog post on how to set it up using wcf to build fast and lean web apps 这是有关如何使用wcf进行设置以构建快速,精简的Web应用程序的博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM