[英]How can I send an array of string from C# to client side?
How can I send an array of string from ASP.NET(C#) to javascript using the jQuery ajax technique ?? 我如何使用jQuery ajax技术从ASP.NET(C#)向javascript发送字符串数组?
I know how to do it for a normal string, but i need to implement it with an array of string. 我知道如何为普通字符串执行此操作,但是我需要使用字符串数组来实现它。
can I use the ' Response.Write
' with an array ?? 我可以将' Response.Write
'与数组一起使用吗? if yes, then how can I read it from the client side ?? 如果是,那我该如何从客户端读取它呢? how can i read array from there ?? 我如何从那里读取数组?
this is from server side : 这是从服务器端开始的:
protected void Page_Load(object sender, EventArgs e)
{
string[] arr1 = new string[] { "one", "two", "three" };
Response.Write(arr1);
Response.End();
}
This is from the client side: 这是从客户端:
$(document).ready(function(){
$(':button').click(function(){
var text_result = "ok"
$.post('default.aspx', { text_res: text_result} , function(data){
alert("The result is: "+data);
}).error(function(){
alert("Error is occured");
});
});
});
this is not working(for array), but its working for normal data Thanks alot 这是行不通的(对于数组),但它对正常数据有效
Try this 尝试这个
Front end 前端
var ID = 1;
$.ajax({
type: "POST",
url: "defaut.aspx/GetCoolArray",
data: "{ID:" + ID + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var arrayresult = new Array();
arrayresult = msg.d;
// Do whatever with this cool array
},
error: function (msg) {
alert(msg.status +" : "+ msg.statusText);
}
});
}
Back end 后端
[WebMethod]
public static string[] GetCoolArray(string ID)
{
string[] rValue = new string[2];
rValue[0] = "Cool array element 1";
rValue[1] = "Cool array element 2";
return rValue;
}
Use the string.Join
method 使用string.Join
方法
protected void Page_Load(object sender, EventArgs e)
{
string[] arr1 = new string[] { "one", "two", "three" };
string result=String.Join(",",arr1);
Response.Write(result);
Response.End();
}
This will send the result back to client as "one, two, three"
这会将结果作为"one, two, three"
发送回客户端
And in your client Side code, you can get it and then apply the split
function and you will get an array. 在客户端代码中,您可以获取它,然后应用split
函数,您将获得一个数组。
$(function () {
$(':button').click(function(){
var text_result="ok";
$.post("@Url.Action(defaut.aspx/GetCoolArray",
{ text_res: text_result}function(msg) {
var arr=msg.split(",");
$.each(arr,function(index,item){
alert(item);
});
});
});
});
You can also Send the DATA as JSON
to the client. 您也可以将DATA作为JSON
发送到客户端。 You may use JavaScriptSerializer
/ JSON.NET
library to do so. 您可以使用JavaScriptSerializer
/ JSON.NET
库来执行此操作。 In that case, you may return a valid JSON
like this 在这种情况下,您可能会返回这样的有效JSON
[
"One",
"Two",
"three"
]
If you are simply GETting some data, you may use the getJSON
method. 如果只是获取一些数据,则可以使用getJSON
方法。
$.getJSON("",function(msg){
$.each(msg,function(index,item){
alert(item)
});
});
The answer dependes on what are going to do with those strings. 答案取决于这些字符串将要做什么。 If you want to print them, then just use string.Join(", ", arr1)
. 如果要打印它们,则只需使用string.Join(", ", arr1)
。 If you want to process them, the best way will be serializing them as a JSON object (see here or here ) 如果要处理它们,最好的方法是将它们序列化为JSON对象(请参阅此处或此处 )
I am pretty sure you can't. 我很确定你不会。 What I usually do when I have to send a dictionary or an array from server to client, I create a string and separate all the elements from an array with | 当我必须将字典或数组从服务器发送到客户端时,通常要做的是创建一个字符串,并使用|分隔数组中的所有元素。 in your case it would be "one|two|three" just remember to use HttpUtility.UrlEncode() on each element so it won't have a escape character to break your data. 在您的情况下,它应该是“ one | two | three”,只记得在每个元素上使用HttpUtility.UrlEncode(),这样它就不会有转义符来破坏您的数据。
string strReturn = string.Empty;
foreach(string strArray in arr1)
{
strReturn += HttpUtility.UrlEncode(strArray) + "|";
}
Response.Write(strReturn.Substring(0, strReturn.Length - 1);
And on client : 在客户端上:
function(data) {
data = (data || "");
var arrData = unescape(data).split('|');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.