0

我有一个从 jquery 生成的字符串,例如string1,string2,string3,stringn。我需要使用 jquery 将此数据提交到另一个将处理字符串的 asp 页面。如何将此字符串获取到 C# 代码?我想使用文件后面的代码来处理这个逗号分隔的列表。在 ASP.NET\C# 中非常新

4

3 回答 3

1

您需要向您的 asp.net 应用程序发出 AJAX 请求。jQuery 有一个 $.ajax() 方法可以帮助您更轻松地完成此操作。

您也可以使用传统的“表单”,让 jquery 将数据粘贴在隐藏字段中并触发提交。

于 2012-08-21T10:03:26.050 回答
1

您将需要使用 jquery post 方法,如下所示:

$.post('File Address', {data : your String});

然后在 asp.net 页面中检索它。

于 2012-08-21T10:16:54.260 回答
1

首先,您需要将 JSON 发布到服务器:为此,您可以有机会通过 $.ajax() 发送,或者您可以像这样访问 PageMethod:(这也是给定页面上的 javascript 代码)

 //obj is the object what you have on the clientside. (f.e. an array of strings)
 var jsonString = JSON.stringify(obj, '');

 //in this example the method's name is LoadItems and the IfSuccess 
 //and IfError methods for callbacks
 PageMethods.LoadItems(jsonString, this.IfSuccess, this.IfError, this);

在服务器端之后,如果您只有字符串,则需要将 json 反序列化为指定的类、object[] 或 string[]。这是您的案例的示例:

            [WebMethod]
            public static string LoadStrings(string jsonString)
            {
                try
                {
                    JavaScriptSerializer s = new JavaScriptSerializer();

                    string[] stringArray = s.Deserialize<string[]>(jsonString);
                }
                ...
            }

使用此解决方案,您可以访问 stringArray 中的字符串。

于 2012-08-21T10:49:19.730 回答