Ajax 调用自写的webservice 1


1、新建一个AJAX Control Toolkit Website
2、把CarsService.xml(相当一数据库)添加到项目的App_Data文件夹。
3、“添加新项”--“CarsService.asmx”,在CarsService.cs添加代码:
using System.Xml;

using System.Web.Services;
using System.Collections.Specialized;
using AjaxControlToolkit;
[System.Web.Script.Services.ScriptService()]
    private static XmlDocument _document;
    private static object _lock = new object();
-------
  public static XmlDocument Document
    {
       //code...
    }
-------
    public static string[] Hierarchy
    {
     //code...
    }

4、然后把原来的HelloWorld方法改为    GetDropDownContents()方法


5、在Default.aspx添加一个updatepanel,再在updatepanel添加三个Dropdownlist
、三个CascadingDropDown和一个Label,把Dropdownlist3的“启用AutoPastBack”选中。

6、在Default.aspx.cs添加代码:
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string

knownCategoryValues, string category)
    {
        return new CarsService().GetDropDownContents(knownCategoryValues, category);
    }
------------
7、CascadingDropDown1的属性窗口,把TargetControlID设为"DropDownList1";
CascadingDropDown2的TargetControlID设为"DropDownList2";
CascadingDropDown3的TargetControlID设为"DropDownList3"

8、把DropDownList1的Category设为"Make",LoadingText设为"[Loading makes...]",
PromptText设为"Please select a make",ServiceMethod设为"GetDropDownContents",
ServicePath设为"CarsService.asmx" ;DropDownList2和DropDownList3的设置类似:
具体看代码:

        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server"

TargetControlID="DropDownList1"
            Category="Make"  PromptText="Please select a make"  LoadingText="[Loading makes...]"
            ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents" />
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server"

TargetControlID="DropDownList2"
            Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
            ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server"

TargetControlID="DropDownList3"
            Category="Color" PromptText="Please select a color" LoadingText="[Loading colors...]"
            ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents"
            ParentControlID="DropDownList2" />
-------

9、在在Default.aspx.cs添加代码:
 protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        // code...
}

10、CarsService.cs的代码:

11、现在还不是开香槟的时候,因为当你选择了下拉框后Label却没有反馈东西,There Must Something Wrong!需要在Default.aspx的第一行添加代码:
 EnableEventValidation="false" 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 AJAX 调用 WebService 有以下几个步骤: 1. 在 HTML 页面中引入 jQuery 库(如果项目中已经引入了 jQuery,则可以跳过此步骤)。 ```html <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> ``` 2. 在 JavaScript 中编写 AJAX 调用代码。下面是一个示例: ```javascript $(document).ready(function() { $("#btnSearch").click(function() { var keyword = $("#txtKeyword").val(); $.ajax({ type: "POST", url: "WebService.asmx/GetSearchResult", data: "{'keyword':'" + keyword + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { $("#searchResult").html(data.d); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); } }); }); }); ``` 解释一下上面代码中的各个参数: * `type`:请求方式,可以是 "GET" 或 "POST"。 * `url`:WebService 的地址,需要根据实际情况修改。 * `data`:请求参数,需要按照指定的格式传递,这里使用的是 JSON 格式。 * `contentType`:请求数据类型,需要设置为 "application/json; charset=utf-8"。 * `dataType`:响应数据类型,需要设置为 "json"。 * `success`:请求成功后的回调函数。 * `error`:请求失败后的回调函数。 3. 在 WebService 中编写对应的方法。例如,上面代码中调用的是 `GetSearchResult` 方法,那么在 WebService 中就需要定义这个方法,代码如下: ```csharp [WebMethod] public string GetSearchResult(string keyword) { // 根据关键字查询数据库,并返回查询结果 return "查询结果"; } ``` 注意,方法需要添加 `[WebMethod]` 标记,同时需要设置返回值类型和参数类型。 这样就完成了 AJAX 调用 WebService 的过程。需要注意的是,由于 AJAX 调用是异步的,因此需要在回调函数中更新页面的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值