JQuery AJAX получить запрос веб-метода aspx, не достигнув точки останова в методе, и ошибка в случае разбора JSON не удалось - PullRequest
0 голосов
/ 13 сентября 2018
[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);
                }
            })
        })
    });

Я пытаюсь вызвать webmethod, используя jquery ajax-тип: "GET", первая проблема в том, что точка останова в методе не задана. и во-вторых, в браузере я получаю сообщение об ошибке "Запрошенный анализ JSON не выполнен". Что делать, пожалуйста, помогите ...

1 Ответ

0 голосов
/ 13 сентября 2018

Измените ваш метод с GET на POST в jQuery

$("#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)]
...