【问题标题】:Webservice method not found by JQuery Ajax-Request error 500JQuery Ajax-Request 错误 500 未找到 Web 服务方法
【发布时间】:2017-06-10 23:59:12
【问题描述】:

JS:

function showComments(couponId) {
            var jsontxt = JSON.stringify({ couponId });
            $.ajax({
                type: "POST",
                url: "<% =Page.ResolveUrl("~/Admin/UserCoupons.aspx/GetComments") %>",
                data: jsontxt,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                //async:true,
                success: function (result) {
                    alert("We returned: " + result.d);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });

        }

C#:

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetComments(string couponId)
        {
            //will return precomipled generated html for display in div
            return "Success";

        }

C# 代码位于 .cs 文件后面的页面代码中 在使用堆栈溢出之前我已经解决了这一切 感谢这里的专家。 不过这次还有点别的事

我在 page_load 方法中获取调试指针,然后在母版页的页面加载方法中。但调试不进入 GetComments

遇到异常

未知的网络方法 GetComments。 参数名称:方​​法名

下面是堆栈跟踪

   at System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)
   at System.Web.Script.Services.RestHandler.CreateHandler(WebServiceData webServiceData, String methodName)
   at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
   at System.Web.Script.Services.RestHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
   at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
   at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

【问题讨论】:

  • JSON.stringify({ couponId }); 没有意义.... 你会有{ "foo" }
  • @Sadiqabbas Hirani 试试这个 var jsontxt = JSON.stringify({ couponId: val }); //value需要传入val,couponId是参数

标签: javascript c# jquery ajax web-services


【解决方案1】:

你的ajax数据应该是JSON.stringify({ couponId:'123' }) //参数名&值。

function showComments(couponId) {
            var jsontxt = JSON.stringify({ couponId:'123' });
            $.ajax({
                type: "POST",
                url: "<% =Page.ResolveUrl("~/Admin/UserCoupons.aspx/GetComments") %>",
                data: jsontxt,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                //async:true,
                success: function (result) {
                    alert("We returned: " + result.d);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });

        }

【讨论】:

  • couponId 我作为参数获取
  • 而且我已经将 "{"couponId":"123"}" 作为 jsontxt 的值
【解决方案2】:

经过长时间的谷歌和所有其他人并应用所有可能的解决方案。最后,我找到了解决方案。那就是 在 ~/App_start/RouteConfig.cs 编辑文件

替换

settings.AutoRedirectMode = RedirectMode.Permanent;

settings.AutoRedirectMode = RedirectMode.Off;

这解决了我的问题。感谢所有感兴趣并表现出他们的慷慨的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2019-12-10
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多