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;
}
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.