我的 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/