【问题标题】:Failed to load resource on web method AJAX无法在 Web 方法 AJAX 上加载资源
【发布时间】:2017-09-12 05:11:43
【问题描述】:

我正在尝试在我的代码中使用 AJAX 并调用这样的网络方法。

function generate_source(year_source, month_source) {
            var gData_source = '';
            if (year_source) {
                gData_source = [];
                gData_source[0] = year_source;
                gData_source[1] = month_source;
                console.log('first part');
            }
            else {
                var d_source = new Date();
                gData_source = [];
                gData_source[0] = d_source.getFullYear();
                gData_source[1] = d_source.getMonth() + 1;
                console.log('second part');


            }
            var jsonData_source = JSON.stringify({
                gData_source: gData_source
            });


            var ctx = document.getElementById("order_source").getContext('2d');
            $.ajax({

                url: "dashboard.aspx/getordersource",
                data: jsonData_source,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function () {
                    $("#loader_divsource").show();
                },
                success: function (response) {
                    $("#loader_divsource").hide();
                    var chartLabel = eval(response.d[0]); //Labels
                    var chartData = eval(response.d[1]); //Data

                    var myChart = new Chart(ctx, {
                        type: 'doughnut',
                        data: {

                            labels: chartLabel,
                            datasets: [
                                {
                                    type: 'doughnut',
                                    label: chartLabel,
                                    data: chartData,
                                    backgroundColor: [
                                         "#FF6384",
                                         "#36A2EB",
                                    ],
                                    hoverBackgroundColor: [
                                        "#FF6384",
                                        "#36A2EB",
                                    ]

                                }

                            ]
                        }
                    });

                }

            });


        }
        var d_source = new Date();
        gData_source = [];
        $('#year_source').val(d.getFullYear());
        $('#month_source').val(d.getMonth() + 1);
        generate_source('', '');

我的web方法是这样的;

[System.Web.Services.WebMethod]
    public static List<string> getordersource(List<int> gData)
    {
        DataSet ds = ws_db.get_Dataset_order_source();
        var returnData = new List<string>();

     ......
        return returnData;

    }

每当我尝试运行此数据时,我的 web 方法断点都不会被命中。此外,如果我在没有数据的情况下使用相同的方法,我不会收到此错误。快把我逼疯了。

【问题讨论】:

  • 您检查控制台是否有任何错误?
  • 是的,它给了我“加载资源失败:服务器响应状态为 500(内部服务器错误)”
  • “如果我在没有数据的情况下使用相同的方法,我不会收到此错误”是什么意思?
  • 如果我没有在调用中传递 json 数据并更新我的 web 方法以不带参数,它工作正常。

标签: c# jquery asp.net ajax webmethod


【解决方案1】:

我认为您的问题出在这段代码中:

var jsonData_source = JSON.stringify({
                gData_source: gData_source
            });

您正在尝试使用无效的键值对序列化数组。 改成这样:

var jsonData_source = JSON.stringify(gData_source);

你的网络方法也应该是这样的:

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.JSON)]
    // just return a string, not list, your JSON string should have contain all your enumerable in your string Data
    public static string getordersource(List<int> gData)
    {
        DataSet ds = ws_db.get_Dataset_order_source();
        JsonSerializer serializer = new JsonSerializer();
        var returnData = serializer.serialize(ds);

     ......
        return returnData;

    }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    相关资源
    最近更新 更多