将数组从 jquery ajax 方法传递到 C# 代码隐藏

发布于 2024-12-09 18:03:56 字数 1603 浏览 2 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-12-16 18:03:56

有两种方法可以从客户端访问后台代码。

  1. 将其存储在随请求发送的集合中(通常这是通过表单提交)。
  2. 通过 JavaScript 设置对服务器端服务的“AJAX”调用。

上述内容有多种变化,但本质上您正在使用服务或正在回发。

查看您的代码,您想要 AJAX 方向。我将从 这篇 Stack Overflow 帖子开始,因为它涵盖了将数组传递回代码隐藏文件的“服务端点”的基础知识。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文