zoukankan      html  css  js  c++  java
  • 用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>

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    html <a> 标记 颜色与下划线 muddy
    sharepoint 获得当前登录人的用户名(转载) muddy
    AspNetPager+GridView分页实现
    UpdatePanel的简单用法(1)
    LINQ to SQL系列三 使用DeferredLoadingEnabled,DataLoadOption指定加载选项
    Custom Sharepoint Lookup Field
    Gridview使用LINQ分页例子
    合并GridView中某行相同信息的行
    初学LINQ增删改查(一)
    ASP.NET购物车
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519241.html
Copyright © 2011-2022 走看看