WebMethod 未被调用

发布于 2024-12-02 21:14:06 字数 562 浏览 0 评论 0原文

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

I am passing a javascript variable containing a string to the server via jquery.ajax. Although the "success" condition is called, the server-side WebMethod is never called. Client:

 $.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); }
        })

Server:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-09 21:14:06

请尝试对您的 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 值更改为字符串。

Try following fixes for your Ajax request:

 $.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); }
        })

Notice changed dataType and data value as a string.

极致的悲 2024-12-09 21:14:06

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

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

I have encountered the same issue. After Googling, I found the solution, and it works for me. Navigate to RouteConfig.cs and comment out the line below:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}
江南月 2024-12-09 21:14:06

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

i would want to add one note: you will have data error of your "ID" (or another field) string contains quotes like = '.
solve this issue:

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
                    }
                });
微暖i 2024-12-09 21:14:06

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

Try like this:
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";
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文