javascript - 未正确构建 jQuery ajax 数据

标签 javascript jquery asp.net ajax

我的 aspx 页面中有以下 js 代码:

                    $.ajax({
                        type: 'POST',
                        url: '/Reporting/landing.aspx/UpdateUserReportingSettings',
                        data: "{ 'reportingSettings' :" + columns.join() + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        beforeSend: function (xhr, opts) {
                        },
                        success: function (data) {
                            window.top.location.href = "landing.aspx";
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('Error Message' + thrownError);
                            alert('error' + xhr.responseText);
                       }
                    });

列是在此之上构建的,如下所示:

                    $('#currentColumnsList').each(function () {
                        // this is inner scope, in reference to the .phrase element
                        var column = '';
                        $(this).find('li').each(function () {
                            // cache jquery var
                            var current = $(this);
                            // check if our current li has children (sub elements)
                            // if it does, skip it
                            // ps, you can work with this by seeing if the first child
                            // is a UL with blank inside and odd your custom BLANK text
                            if (current.children().size() > 0) {
                                return true;
                            }
                            // add current text to our current phrase
                            column += (current.text() + ',');
                        });
                        // now that our current phrase is completely build we add it to our outer array
                        columns.push(column);
                    });

然后我在代码隐藏页面上有一个 Web 方法,如下所示:

    [WebMethod]
    public static void UpdateUserReportingSettings(string reportingSettings)
    {
        string columns = reportingSettings;

        //more code
    }

如果我按如下方式更改数据行,我可以在 webmethod 中命中断点,并且 reportingSettings 字符串将按预期进行测试:

data: "{ 'reportingSettings' : 'test' }",

如果我提醒 columns.join() - 我得到了逗号分隔值某些行 columnA、columnB 等 - 将其传递到 reportingSettings 字符串中的代码隐藏 WebMethod 的最佳方法是什么?

最佳答案

columns.join() 之前缺少 ' 符号。应该是:

data: "{ 'reportingSettings' : '" + columns.join() + "'}"

关于javascript - 未正确构建 jQuery ajax 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218333/

相关文章:

javascript - 在 firebase 云函数中使用 set timeout 函数是否明智?

javascript - 如何在 Express.js 中正确使用 Node-mysql?

jquery - 切换类(class)效果不佳

javascript - IE8 HTML5 动态内容没有应用 AngularJs 的样式

sql - 使用c#在sql server 2012中存储日期和时间

javascript - 如何修复javascript函数中的 "MasterPage is undefined"?

javascript - JQuery ajax 函数发送正确的值。但 RestController 接收 null

javascript - 如何为 new Audio() 使用 flash 后端?

javascript - qTip2 - 激活时刷新工具提示

c# - 如何在 .NET Core 中实现 DbContext 连接字符串?