将数组从 jquery ajax 方法传递到 C# 代码隐藏
我需要将 JavaScript 数组变量传递给代码隐藏文件并访问该数组。
请告诉我这是否正是 Ajax 方法所期望的数据对象。使用此功能时,代码始终跳转到 failureCallback
函数。谁能帮我解决这个问题吗?
jQuery/JavaScript:
result
数组中的数据为:section_1,section_2,section_3
。
paramList
的输出为:{"data":"section_1,section_2,section_3"}
。
function generateData() {
var result = $('#accordion').sortable('toArray');
alert(result);
ExecutePageMethod("ReorderList.aspx", "HandleData", ["data", result], successCallback, failureCallback);
}
function ExecutePageMethod(page, fn, paramArray, successFn, errorFn) {
alert("entered page method");
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
alert(paramList);
$.ajax({
type: "POST",
url: page + "/" + fn,
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
function successCallback(result) {
var parsedResult = jQuery.parseJSON(result.d);
}
function failureCallback(result) {
alert("entered failure");
}
C# 代码背后:
public static string HandleData(object[] data)
{
//How should I parse this object data?
return data;
}
I need to pass a JavaScript Array variable to a code-behind file and access that array.
Please let me know if this is the exact data object that the Ajax method would expect. While using this, the code always jumps to failureCallback
function. Can anyone please help me with this?
jQuery/JavaScript:
The data in the result
array is: section_1,section_2,section_3
.
The output of paramList
is: {"data":"section_1,section_2,section_3"}
.
function generateData() {
var result = $('#accordion').sortable('toArray');
alert(result);
ExecutePageMethod("ReorderList.aspx", "HandleData", ["data", result], successCallback, failureCallback);
}
function ExecutePageMethod(page, fn, paramArray, successFn, errorFn) {
alert("entered page method");
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
alert(paramList);
$.ajax({
type: "POST",
url: page + "/" + fn,
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
function successCallback(result) {
var parsedResult = jQuery.parseJSON(result.d);
}
function failureCallback(result) {
alert("entered failure");
}
C# Code Behind:
public static string HandleData(object[] data)
{
//How should I parse this object data?
return data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法可以从客户端访问后台代码。
上述内容有多种变化,但本质上您正在使用服务或正在回发。
查看您的代码,您想要 AJAX 方向。我将从 这篇 Stack Overflow 帖子开始,因为它涵盖了将数组传递回代码隐藏文件的“服务端点”的基础知识。
There are two ways to access code behind from the client.
There are variations on the above, but essentially you are consuming a service or you are posting back.
Looking at your code, you want the AJAX direction. I would start with this Stack Overflow post, as it covers the basics of passing an array back to the "service endpoint" of a code behind file.