javascript 将数组传递给 jquery ajax 后面的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15699395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:46:17  来源:igfitidea点击:

Pass array to code behind from jquery ajax

javascriptjqueryasp.netajax

提问by Rajaram Shelar

I have to pass two dimensional array to the page method written at code behind of the web page in asp.net I have a variable objListas a two dimensional array. I used following code for achieving this with no success and page method is not called.

我必须将二维数组传递给在 asp.net 中网页后面的代码中编写的页面方法我有一个变量objList作为二维数组。我使用以下代码实现此目的但没有成功,并且未调用页面方法。

JAVASCRIPT

爪哇脚本

function BindTable(objList) {

    $.ajax(
    {
           url: "CompCommonQues.aspx/SaveData",
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           type: "POST",
           data: { data: objList },
           success: function (data) {
           //Success code here
    },
    error: function () { }
    });
  }

CODE BEHIND .CS File

.CS 文件背后的代码

 [WebMethod]
public static string SaveData(string[,] data)
{
    string[,] mystring = data;
    return "saved";
}

There is method like JSON.stringify(objList) to pass the json array to code behind but couldn't implement this. A simple call wihtout array working for me like

有像 JSON.stringify(objList) 这样的方法可以将 json 数组传递给后面的代码,但无法实现。一个没有数组的简单调用对我来说像

data: "{ 'data':'this is string' }",

and in code behind

并在后面的代码中

[WebMethod]
public static string SaveData(string data)
{
    string mystring = data;
    return "saved";
}

There is problem in passing data. Can you assist me how to pass it for arrays?

传球有问题data。你能帮我如何将它传递给数组吗?

采纳答案by CyclingFreak

Try the correct JSON notation in JavaScript

在 JavaScript 中尝试正确的 JSON 表示法

var objList = new Array();
objList.push(new Array("a","b"));
objList.push(new Array("a", "b"));
objList.push(new Array("a", "b"));

   $.ajax({
       type: "POST",
       url: "copyproduct.aspx/SaveDate",
       data: "{'data':'" + JSON.stringify(objList) + "'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (msg) {
            alert(msg.d);
       }
   });

In code behind, you can deserialize with JavaScriptSerializer (System.Web.Script.Serialization)

在后面的代码中,您可以使用 JavaScriptSerializer (System.Web.Script.Serialization) 进行反序列化

[WebMethod()]
public static string SaveDate(string data)
{
    JavaScriptSerializer json = new JavaScriptSerializer();
    List<string[]> mystring = json.Deserialize<List<string[]>>(data);
    return "saved";
}

I had to deserialize to a generic list of a string array because you can't deserialize to a string (check: http://forums.asp.net/t/1713640.aspx/1)

我不得不反序列化为字符串数组的通用列表,因为您无法反序列化为字符串(检查:http: //forums.asp.net/t/1713640.aspx/1