javascript 如何调用没有参数的c#方法并访问返回的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24500744/
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
How to call c# method with no parameters and access returned data?
提问by loonyuni
So I have seen many examples such as these : https://stackoverflow.com/a/8094230/2525507
所以我见过很多这样的例子:https: //stackoverflow.com/a/8094230/2525507
public class WebService : System.Web.Services.WebService {
[WebMethod]
public List<string> getList() {
return new List<string> {"I", "Like", "Stack", "Overflow"};
}
}
Where you just it seems that through the success function you can view the returned data from the c# method in the form of an alert. But what if I want to access this "input+1" data outside of the function call, how would I proceed to do that? Also I am not sure how to call a method that has no parameters?
在您看来,通过成功函数,您可以以警报的形式查看从 c# 方法返回的数据。但是如果我想在函数调用之外访问这个“输入+1”数据,我将如何进行呢?另外我不确定如何调用没有参数的方法?
<body>
<select id="wordSelect">
// Drop Down Menu to be populated
</select>
<script>
$(function () {
$.ajax({
url: 'WebService.asmx/getList',
data: '{**NO PARAMETERS?!**}', // should I also call JSON.stringify?
type: 'POST',
dataType: 'json',
contentType: 'application/json',
success: function (data, status) {
alert(data);
alert(typeof data);
}
});
});
$.each(data.i, function(index, item) { // will this access "I", "Like", ... etc?
$(#wordSelect).append(
$("<option></option>")
.text(item)
);
};
</script>
</body>
In the end, I would like to populate a dropdown list using returned JSON data from a c# method that has been called through ajax, but I'm not sure how I can play with that retrieved JSON data that seems to be stuck in the function call?
最后,我想使用通过 ajax 调用的 ac# 方法返回的 JSON 数据填充下拉列表,但我不确定如何处理似乎卡在函数中的检索到的 JSON 数据称呼?
Sorry, I am new to Jquery/AJAX/etc... But Thank you so much!
抱歉,我是 Jquery/AJAX/etc 的新手……但非常感谢!
回答by Kyle Gobel
If your method takes no arguments, just don't specify the data property on the ajax call
如果您的方法不带任何参数,请不要在 ajax 调用中指定 data 属性
<script>
$(function () {
$.ajax({
url: 'WebService.asmx/getList',
type: 'POST',
dataType: 'json', //make sure your service is actually returning json here
contentType: 'application/json',
success: function (data, status) {
//here data is whatever your WebService.asmx/getList returned
//populate your dropdown here with your $.each w/e
}
});
});
</script>
Also I could be wrong, but the WebService method you showed, doesn't look like it will return json. I think you will have to serialize, or set the content type or something similar. (Been along time since I've used the asmx type services)
我也可能是错的,但是您展示的 WebService 方法看起来不会返回 json。我认为你将不得不序列化,或设置内容类型或类似的东西。(自从我使用 asmx 类型服务以来已经很久了)
回答by David Robbins
See my answer to this post. I reference a web site called Encosia, written by Dave Ward. He has an excellent series on using Ajax with ASP.net / MVC. That is a great place to start, with numerous examples.
请参阅我对这篇文章的回答。我参考了 Dave Ward 编写的名为 Encosia 的网站。他有一个关于在 ASP.net/MVC 中使用 Ajax 的优秀系列。这是一个很好的起点,有很多例子。