javascript 来自 C# 函数的 JQuery ajax 返回列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26723887/
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
JQuery ajax return list from c# function
提问by wheatfairies
I'm a bit confused when trying to implement C# and jquery to work together. I have a .cs file and a javascript document in the same solution/project. My c# function returns a list of strings, which I would like to append to some HTML data using Javascript. I am also using a webform for my HTML. Is it possible to return my list on the javascript side?
在尝试实现 C# 和 jquery 协同工作时,我有点困惑。我在同一个解决方案/项目中有一个 .cs 文件和一个 javascript 文档。我的 c# 函数返回一个字符串列表,我想使用 Javascript 将其附加到一些 HTML 数据中。我还为我的 HTML 使用了网络表单。是否可以在 javascript 端返回我的列表?
javascript
javascript
$(function () {
$.ajax({
type: "GET",
url: "Test/GetListData",
dataType: "data: Array<any>" //Something like this?
});
//Can i return the list and use it here?
});
c# method
c#方法
public List<string> GetListData()
{
List<string> mylist = new List<string>();
mylist.Add("test1");
mylist.Add("test2");
mylist.Add("test3");
return mylist;
}
采纳答案by wheatfairies
Well after careful research I realized that I was going about my problem the wrong way. I wanted to have C# communicate with javascript/html for a web form in the same project. Using a list was a bad idea as my javascript couldnt effectively see it unless I formatted it as one big JSON String. Here is the updated code that solved my problem, hopefully it helps someone down the line. [ComVisible(true)] allows the external window named test to see my c# function, and the $.parseJSON(myarray) was able to parse my JSON string into a usable array.
经过仔细研究后,我意识到我正在以错误的方式解决我的问题。我想让 C# 与 javascript/html 通信以获取同一项目中的 Web 表单。使用列表是一个坏主意,因为我的 javascript 无法有效地看到它,除非我将它格式化为一个大的 JSON 字符串。这是解决我的问题的更新代码,希望它可以帮助某人。[ComVisible(true)] 允许名为 test 的外部窗口查看我的 c# 函数,并且 $.parseJSON(myarray) 能够将我的 JSON 字符串解析为可用数组。
c#
C#
[ComVisible(true)]
public string GetData()
{
string test = "[{"Name": "test1"}, {"Name": test2"}, {"Name": "test3"}]";
return test;
}
javascript
javascript
<script type="text/javascript">
var test = window.external;
var myarray = test.GetData();
var obj = $.parseJSON(myarray);
alert(obj[1].Name);
}
</script>
回答by Michael Anthopolous
Pulled from this thread:
You can serialize that list into some nice tidy JSON like this:
您可以将该列表序列化为一些整洁的 JSON,如下所示:
using System.Web.Script.Serialization;
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(aList);
回答by Steve Wellens
Try something like this (Note: I had a file already setup so the name is different):
尝试这样的事情(注意:我已经设置了一个文件,所以名称不同):
<script type="text/javascript">
$(function ()
{
$.ajax({
url: "WebService.asmx/GetListData",
success: OnSuccess,
error: OnError
});
});
function OnSuccess(data)
{
for (var i = 1; i < data.all.length; i++)
{
alert(data.all[i].textContent);
}
}
function OnError(data)
{
alert(data.statusText);
}
</script>