WebMethod没有被调用

时间:2011-08-31 20:25:42

标签: c# asp.net jquery webmethod

我通过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");
    }

4 个答案:

答案 0 :(得分:6)

尝试以下针对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值更改为字符串。

答案 1 :(得分:3)

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

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

答案 2 :(得分:0)

我想添加一个注释:您的“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
                    }
                });

答案 3 :(得分:0)

试试这样: 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";
        }