Jquery ajax get request to aspx web method not hit the breakpoint in the method and error is json pa

Posted

技术标签:

【中文标题】Jquery ajax get request to aspx web method not hit the breakpoint in the method and error is json parse failed【英文标题】:Jquery ajax get request to aspx web method not hitting the breakpoint in the method and error is json parse failed 【发布时间】:2019-02-18 00:27:26 【问题描述】:
[WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string getJsonString()
    
        Person p = new Person();
        p.name = "Alex";
        p.address = "UK";
        string jsonString;
        jsonString = JsonConvert.SerializeObject(p);

        return jsonString;
    





$("#clickme").on('click', function () 
          $.ajax(
                type:"GET",
                url: "JsonPage.aspx/getJsonString",
                dataType: "json",
                contentType: "application/json; charset=utf-8",


                success: function (response) 
                    debugger;

                    $("#name").text(response.d.name);
                    $("#address").text(response.d.address);
                ,
                error: function (jqXHR, exception) 
                    var msg = '';
                    if (jqXHR.status === 0) 
                        msg = 'Not connect.\n Verify Network.';
                     else if (jqXHR.status == 404) 
                        msg = 'Requested page not found. [404]';
                     else if (jqXHR.status == 500) 
                        msg = 'Internal Server Error [500].';
                     else if (exception === 'parsererror') 
                        msg = 'Requested JSON parse failed.';
                     else if (exception === 'timeout') 
                        msg = 'Time out error.';
                     else if (exception === 'abort') 
                        msg = 'Ajax request aborted.';
                     else 
                        msg = 'Uncaught Error.\n' + jqXHR.responseText;
                    
                    alert(msg);
                
            )
        )
    );

我正在尝试使用 jquery ajax 类型调用 webmethod:“GET”,第一个问题是方法中的断点没有命中。第二个在浏览器中我收到错误“requested json parse failed”。该怎么办请帮助...

【问题讨论】:

从您的请求中删除 contentType: "application/json; charset=utf-8",这仅在您向服务器发送一些数据(例如 POST)时才有用 需要解析ajax响应:var person = JSON.parse(response.d) 好的,我已经做出了这个改变。问题仍然存在。实际上解析是第二个问题。第一个问题是,webmethod 没有命中。我不知道为什么。 可以肯定的是,您的clickme 函数在$(document).ready(function () 中? 是的,它在 jQuery 就绪函数中。 【参考方案1】:

在 jQuery 中将方法从 GET 更改为 POST

$("#clickme").on('click', function () 
        $.ajax(
            url: "Default.aspx/getJsonString",
            data: "",
            contentType: "application/json; charset=utf-8",
            type: "POST",
            success: function (response) 
                debugger;
                $("#name").text(response.d.name);
                $("#address").text(response.d.address);
            ,
            error: function (jqXHR, exception) 
                var msg = '';
                if (jqXHR.status === 0) 
                    msg = 'Not connect.\n Verify Network.';
                 else if (jqXHR.status == 404) 
                    msg = 'Requested page not found. [404]';
                 else if (jqXHR.status == 500) 
                    msg = 'Internal Server Error [500].';
                 else if (exception === 'parsererror') 
                    msg = 'Requested JSON parse failed.';
                 else if (exception === 'timeout') 
                    msg = 'Time out error.';
                 else if (exception === 'abort') 
                    msg = 'Ajax request aborted.';
                 else 
                    msg = 'Uncaught Error.\n' + jqXHR.responseText;
                
                alert(msg);
            
        )
    )

并将您的网络方法更改为

 [ScriptMethod(UseHttpGet = false)]

【讨论】:

我做了这个改变,但什么也没发生。 Arun Kumar,我已经按照你的建议更改了 get to post,但它没有达到 webmethod 中的断点。 转到您的 RouteConfig 并注释此行:settings.AutoRedirectMode = RedirectMode.Permanent; Arun kumar,没有达到断点,仍然是同样的消息“请求的 JSON 解析失败。” 你改web方法使用httpget false了吗?我已经测试了这段代码并为我工作

以上是关于Jquery ajax get request to aspx web method not hit the breakpoint in the method and error is json pa的主要内容,如果未能解决你的问题,请参考以下文章