WebMethod未被调用

本文关键字:调用 WebMethod | 更新日期: 2023-09-27 18:07:06

我通过jquery.ajax传递一个包含字符串的javascript变量给服务器。尽管调用了"成功"条件,但从未调用服务器端WebMethod。客户:

 $.ajax({
            type: "post",
            url: "Playground.aspx/childBind",
            data: {sendData: ID},
            //contentType: "application/json; charset=utf-8",
            dataType: "text",
            success: function (result) { alert("successful!" + result.d); }
        })
服务器:

[WebMethod]
    public static string childBind(string sendData)
    {
        return String.Format("Hello");
    }

WebMethod未被调用

尝试修复您的Ajax请求:

 $.ajax({
            type: "post",
            url: "Playground.aspx/childBind",
            data: "{sendData: '" + ID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) { alert("successful!" + result.d); }
        })

注意将dataTypedata的值更改为字符串

我也遇到过同样的问题。在谷歌上搜索后,我找到了解决方案,它对我很有效。导航到RouteConfig.cs并注释掉下面的行:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}

我想添加一个注意事项:您将有您的"ID"(或另一个字段)字符串包含引号= '的数据错误。解决这个问题:

var DTO = {'sendData': ID};
                $.ajax({
                    "type": "POST",
                    "dataType": 'json',
                    "contentType": "application/json; charset=utf-8",
                    "url": sSource,
                    "data": JSON.stringify(DTO),
                    "success": function (msg) {
                        //do something
                    }
                });

试试这样:JQuery:

                var dataString = JSON.stringify({
                    contractName: contractName,
                    contractNumber: contractNumber
                });
                $.ajax({
                    type: "POST",
                    url: "CreateQuote.aspx/GetCallHistory",
                    data: dataString,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {
                        alert(result);
                            OpenLightBox('divDelete');
                    }
                });

ASPX.CS:

        [System.Web.Services.WebMethod]
        public static string GetCallHistory(string contractName, string contractNumber)
        {
            return "Nalan";
        }