从javascript调用的Web服务中检索值? - c#

是否可以通过从javascript调用网络方法来检索值或日期,这是示例代码:

//This method is in a webservice.asmx file.
[WebMethod]
public List<tbl_City> GetAllCitiesByCountry(int countryID)
{

    return Cities.GetAllCitiesByCountry(CountryID: countryID);
}


<script language="javascript" type="text/javascript">

function fillCities() {
    var dropDownList = document.getElementById('<%=DropDownList_Country.ClientID %>');
    var selectedIndex = dropDownList.selectedIndex;
    var value = dropDownList[selectedIndex].value;

   WebService.GetAllCitiesByCountry(parseInt(value.toString()), onSuccess, null, "");

}

   function onSuccess(result){
      alert(result[0].(PropertyName));
      }

变量x不会检索任何内容,我想它会产生错误。我试图定义一个数组,但仍然无法正常工作。任何的想法 ?

编辑:

上面的代码已被更改,现在是我的问题的答案,下面是使用JQuery的答案。

参考方案

将Json响应与Jquery一起使用,它的确很酷,很容易。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class CitiService : WebService
{
    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<tbl_City> GetAllCitiesByCountry(int countryID)
    {
     List<tbl_City> cities = GetCities(countryID);
     JavaScriptSerializer js = new JavaScriptSerializer();
        var jsonObj = js.Serialize(cities);
        Context.Response.Clear();
        Context.Response.Write(jsonObj);
        Context.Response.End();
     }
 }

在ASp.net页面上

<script language="javascript" type="text/javascript">
 $.ajax({
        url: '<%= ResolveClientUrl("~/CitiService.asmx/GetAllCitiesByCountry") %>',
        dataType: "json",
        data: "{countryID:'100'}",
        success: function (result) {
            alert(result.d.tbl_City.Length) // loop here i.e. foreach to insert in to grid
        }
      });

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

ASP.NET-如何更改JSON序列化的方式? - javascript

我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …

ASP.NET MVC中应为DataTable返回哪种数据? - javascript

我想为DataTable中的每个页面创建动态加载信息。我正在尝试遵循以下示例:https://www.datatables.net/manual/server-sidehttps://www.datatables.net/manual/data来自示例的代码:$('#example').DataTable( { serverSide: t…

javascript popupwindow之后的行如何工作? - javascript

好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…

asp.net mvc中的对象数组数据始终为null - javascript

我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…