开发者

Passing array from jquery ajax method to c# codebehind

开发者 https://www.devze.com 2023-04-12 18:30 出处:网络
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 co

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.

  1. Store it in a collection sent with a request (normally this is through a form submit).
  2. Set up an "AJAX" call to a server side service through JavaScript.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号