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

推荐订阅源

J
Java Code Geeks
IT之家
IT之家
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
V
V2EX
N
Netflix TechBlog - Medium
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
量子位
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
月光博客
月光博客
F
Fortinet All Blogs

博客园 - 朱胜

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>