WebMethod没有被调用

Ser*_*812 5 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); }
        })
Run Code Online (Sandbox Code Playgroud)

服务器:

[WebMethod]
    public static string childBind(string sendData)
    {
        return String.Format("Hello");
    }
Run Code Online (Sandbox Code Playgroud)

And*_*rei 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); }
        })
Run Code Online (Sandbox Code Playgroud)

注意事项已更改dataType,data值为字符串.


小智 6

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

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