惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
博客园_首页
量子位
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
V
Visual Studio Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
D
Docker
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
H
Help Net Security
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale

博客园 - 朱胜

exeCommand 阻止浏览器事件传递 Ext js Grouping 展开和折叠方法 JS获取上传文件的绝对路径,兼容IE和FF - 朱胜 - 博客园 asp.net2.0中异步调用WebService - 朱胜 - 博客园 ASP.Net的正则表达式(RegularExpression) Extjs简介(二) Extjs框架简介(一) ASP.NET 压缩和解压缩 - 朱胜 - 博客园 ASP.NET 恢复备份Sql server - 朱胜 WPF学习2:布局(上) WPF学习笔记1 WPF生命周期和参数配置 WPF编程笔记 0:WPF概况(上部分) JavaScript判断文件大小 - 朱胜 - 博客园 clientHeight,offsetHeight和scrollHeight区别 CSS HACK(ie6-ie7-fox 兼容) 页面禁止后退的方法 upLoad控件设置禁止输入的方法 - 朱胜 - 博客园 [ASP.NET] 实现Label自动换行 - 朱胜 - 博客园
js调用web服务范例 - 朱胜 - 博客园
朱胜 · 2008-12-04 · via 博客园 - 朱胜

1.Web服务

        [WebMethod]
        public string webServiceTest(string userID)
        {
            return userID;
        }

2. 调用方法

RequestByGet("http://www.xxx.com/webService.asmx/webServiceTest?userID=leochu2008",null);

3. 脚本函数

function RequestByGet(url,data)
        {
            var xmlhttp;
            try
            {
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(E)
                {
                    xmlhttp=null;
                }
            }
            if(!xmlhttp&&typeof(XMLHttpRequest)!=="undefined")
            {
                xmlhttp=new XMLHttpRequest();
            }
            xmlhttp.Open("GET",url, false);
            xmlhttp.Send(data);
            var result = xmlhttp.status;
            if(result==200)
            {
                document.write(xmlhttp.responseText);
            }
            xmlhttp = null;
        }
       
        function RequestByPost(url,value)
        {
            var xmlhttp;
            try{
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(E)
                {
                   xmlhttp=null;
                }
            }
            if(!xmlhttp&&typeof(XMLHttpRequest)!=="undefined")
            {
                xmlhttp=new XMLHttpRequest();
            }
            xmlhttp.Open("POST",url, false);
            xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
            xmlhttp.Send(data);
            document.write( xmlhttp.responseText);
        }

如果调用不成功或提示“因 URL 意外地以xxx结束,请求格式无法识别”,要在webservice的 <system.web> 节点下加入
        <webServices>
          <protocols>
              <add   name= "HttpPost "   />
              <add   name= "HttpGet "   />
          </protocols>
</webServices>