传递数组code从jQuery的AJAX背后 [英] Pass array to code behind from jquery ajax

查看:135
本文介绍了传递数组code从jQuery的AJAX背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要过二维数组到页面的方法在asp.net写在code网页的背后我有一个变量 objList 为两二维数组。我用下面的$ C $下没有成功,页面的方法实现这不叫。

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 objList as a two dimensional array. I used following code for achieving this with no success and page method is not called.

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背后cs文件

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

有方法就像JSON.stringify(objList)通过JSON数组背后code,但无法实现这一点。一个简单的wihtout阵列的呼叫工作对我来说就像

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

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

和后面

[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?

推荐答案

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

Try the correct JSON notation in JavaScript

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

在C $ C $的背后,你可以用JavaScriptSerializer(System.Web.Script.Serialization)反序列化

In code behind, you can deserialize with 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";
}

我不得不反序列化到字符串数组的泛型列表,因为你不能反序列化到一个字符串(检查:的 http://forums.asp.net/t/1713640.aspx/1

这篇关于传递数组code从jQuery的AJAX背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆