簡體   English   中英

將jquery字符串傳遞到代碼隱藏文件ASP.NET C#

[英]Pass jquery string to Code Behind file ASP.NET C#

我有一個從jQuery生成的字符串,例如string1,string2,string3,stringn 我需要使用jquery將此數據提交到另一個處理該字符串的asp頁面。 如何將這個字符串添加到C#代碼中? 我想使用文件后面的代碼來處理此逗號分隔的列表。 ASP.NET \\ C#中的新功能

您需要向asp.net應用程序發出AJAX請求。 jQuery有一個$ .ajax()方法來幫助您簡化此過程。

您也可以使用傳統的“表單”,讓jquery將數據粘貼到隱藏字段中並觸發提交。

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

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

然后在asp.net頁面中檢索它。

首先,您需要將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中的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM