您现在的位置: 365建站网 > 365文章 > ajax初探--调用web服务

ajax初探--调用web服务

文章来源:365jz.com     点击数:204    更新时间:2009-09-15 10:05   参与评论
这里我们首先给出这个web服务:

using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web .Script .Services ;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService ]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld(string instr) {
        string str = "服务器asp。net ajax得到了你输入的信息:"+instr +"<br/>你的ip地址是:";
        str += System.Web.HttpContext.Current.Request.UserHostAddress;
        str += "<br/>当前时间:";
        str += System.DateTime.Now.ToLocalTime();
        return str;
    }
   
}

下面是源文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id ="head1" runat="server">
    <title>hello</title>
    <script type ="text/javascript" language ="javascript"  >
    //web服务成功后调用回调函数
    function OnShow(result)//这里的参数是web函数调用后返回的值
    {
    var s=$get("msg");//也可以用document.getElementById("msg")获取html控件“msg”;
  
    s.innerHTML=result .toString ();
    }
    function SayHello()
    {
    var fs=WebService;//这里的WebService是web服务中的类(不要和文件名搞反了)
    fs .HelloWorld(document.getElementById("testmsg").value,OnShow);//OnShow 是web函数调用成功后调用的函数并且把返回值传给它 
  return false ;
    }
    </script>
    <style type="text/css">
        #Text1
        {
            width: 206px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" >
            <Services>
                <asp:ServiceReference Path="WebService.asmx" />//web服务的调用
            </Services>
        </asp:ScriptManager>
        <div style="width: 400px; margin-top :50px; text-align :center ">
            <input id="testmsg" type="text" value="hello ajax" /><br />
            <asp:Button ID="Button1" runat="server" onclientclick="return SayHello()"
                Text="提交给WEB服务" />
            <br />
        </div>
        <div id ="msg" >信息从这里显示。。。。。</div>
    </form>
</body>
</html>

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (204人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号