如何在没有参数的情况下调用c#方法并访问返回的数据?

如何在没有参数的情况下调用c#方法并访问返回的数据?,c#,javascript,jquery,ajax,json,C#,Javascript,Jquery,Ajax,Json,所以我看到了很多这样的例子: 公共类WebService:System.Web.Services.WebService{ [网络方法] 公共列表getList(){ 返回新列表{“I”、“Like”、“Stack”、“Overflow”}; } } 在这里,您似乎可以通过success函数以警报的形式查看从c#方法返回的数据。但是如果我想在函数调用之外访问这个“input+1”数据,我将如何继续呢?我也不知道如何调用一个没有参数的方法 <body> <select id=

所以我看到了很多这样的例子:

公共类WebService:System.Web.Services.WebService{
[网络方法]
公共列表getList(){
返回新列表{“I”、“Like”、“Stack”、“Overflow”};
}
}
在这里,您似乎可以通过success函数以警报的形式查看从c#方法返回的数据。但是如果我想在函数调用之外访问这个“input+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>

//要填充的下拉菜单
$(函数(){
$.ajax({
url:'WebService.asmx/getList',
数据:“{**NO PARAMETERS?!**}”,//我应该也调用JSON.stringify吗?
键入:“POST”,
数据类型:“json”,
contentType:'应用程序/json',
成功:功能(数据、状态){
警报(数据);
警报(数据类型);
}
});
});
$.each(data.i,function(index,item){//这个函数会访问“i”、“Like”等吗?
$(#wordSelect).append(
$("")
.文本(项目)
);
};
最后,我想使用通过ajax调用的c#方法返回的JSON数据填充一个下拉列表,但我不确定如何处理检索到的JSON数据,这些数据似乎卡在函数调用中


抱歉,我不熟悉Jquery/AJAX/etc……但非常感谢您!

如果您的方法不带参数,就不要在AJAX调用中指定数据属性

<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>

$(函数(){
$.ajax({
url:'WebService.asmx/getList',
键入:“POST”,
dataType:'json',//确保您的服务在此处实际返回json
contentType:'应用程序/json',
成功:功能(数据、状态){
//这里的数据是您的WebService.asmx/getList返回的任何内容
//在您的下拉列表中填入您的$。每个w/e
}
});
});

我也可能是错的,但是您展示的WebService方法看起来不会返回json。我认为您必须序列化,或者设置内容类型或类似的内容。(自从我使用asmx类型服务以来,已经很长时间了)

看看我的答案。我参考了一个叫Encosia的网站,它是Dave Ward写的。他有一个关于在ASP.net/MVC中使用Ajax的优秀系列。这是一个很好的起点,有很多例子。

Ajax调用是异步的,所以你必须在
成功
回调中处理所有事情。如果你想在外部做一些事情,您必须准备一些回调,在那里添加代码,并在
success
回调中调用该回调。您能显示consoleThank的webservice的json响应吗?各位!抱歉,这是一个如此琐碎的问题,但我真的无法用正确的术语进行谷歌搜索,也找不到合适的示例。谢谢!谢谢!所以我有了在我的成功调用中尝试只访问数据,但我一直收到一个错误,说“数据”未定义?有什么想法吗?数据是成功回调的第一个参数吗?很好,我的Web服务是嵌套的,我弄乱了url。
<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>