用Xmlhttp无刷新更新DIV

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetTemp(string City)
        {
            return City+"目前温度" + GetRandomNum(20,39);
        }

        public static string GetRandomNum(int min,int max)
        {
            Random randomNum = new Random();

            return randomNum.Next(min,max).ToString();
        }

    }
}

 

 

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head >
    <title></title>
    <script type="text/javascript" >
   
        window.setInterval(btn_click, 3000);
       
       
        function btn_click() {
           
            //GetHttpResult('广州', 'http://localhost:7777/Service1.asmx/GetTemp');

            GetSoapResult('广州', 'http://localhost:7777/Service1.asmx/GetTemp');
          
        }

        function GetRSSResult(url)
        {
            var oRequest;
            try {
            oRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
            try
            {
             oRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                try { oRequest = new XMLHttpRequest(); }
                catch (e) { oRequest = false; }
            }
            }
            oRequest.open("post", url, false);
            oRequest.setRequestHeader("Content-Type", "text/html;charset=utf-8");
            oRequest.send(" ");
            if (window.document.getElementById('answer')!=null)
            window.document.getElementById('answer').innerHTML = oRequest.responseXML.childNodes[1].text;
        }
       
        function GetHttpResult(city, url) {
            var Params = "City=" + city.toString();
            var oRequest;
            try {
            oRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                 oRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                    try { oRequest = new XMLHttpRequest(); }
                    catch (e) { oRequest = false; }
                }
            }
            oRequest.open("post", url, false);
            oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            oRequest.setRequestHeader("Content-Length", Params.length);
            oRequest.send(Params);
            //alert(oRequest.status + "--" + oRequest.statusText + "---" + oRequest.responseXML.childNodes[1].text);
            //alert(oRequest.responseXML.childNodes[1].text);
            if (window.document.getElementById('answer')!=null)
            window.document.getElementById('answer').innerHTML = oRequest.responseXML.childNodes[1].text;

       }

        function addURLParam(URL, paramName, paramValue) {

            URL += URL.indexOf("?") == -1 ? "?" : "&";
            URL += encodeURIComponent(paramName) + "=" + encodeURIComponent(paramValue);
            return URL;
        }

        function GetSoapResult(city,url) {
            //xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            //xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            //xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            //var xmlObj = new ActiveXObject("Msxml2.DOMDocument");
            var sXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            sXml += "<soap:Envelope "
            sXml += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
            sXml += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
            sXml += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
            sXml += "<soap:Body>";
            sXml += "<GetTemp xmlns=\"http://tempuri.org/\">";
            sXml = sXml + "<City>" + city.toString() + "</City>";
            sXml += "</GetTemp></soap:Body></soap:Envelope>"
            //xmlObj.loadXML(sXml);
            alert(sXml);

            var oRequest;
            try {
                oRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    oRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                    try { oRequest = new XMLHttpRequest(); }
                    catch (e) { oRequest = false; }
                }
            }
            oRequest.Open("POST", url, false);
            oRequest.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
            oRequest.setRequestHeader("Content-Length", sXml.length);
            oRequest.setRequestHeader("SOAPAction", "http://tempuri.org/GetTemp");
            oRequest.send(sXml);
            if (window.document.getElementById('answer') != null)
                window.document.getElementById('answer').innerHTML = oRequest.responseText;

            //alert(xmlHTTP.responseXML.childNodes[1].text);
            //alert(xmlResponse.selectSingleNode("soap:Envelope/soap:Body/GetTempResponse/GetTempResult").text);

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" id="btn"  value="GetTEMP"onclick="javascript:btn_click()"/>
    <span id="answer"></span>
    </div>
    </form>
</body>
</html>

 

 

posted @ 2009-07-08 16:20  昕友软件开发  阅读(528)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯