JS调用webservice的通用函数

调用:

RequestByPost(method,variable,value,url,_Namespace)

method:webservice的方法名

variable:webservice的方法的变量名数组

value:webservice的方法的变量的值的数组

url:请求的地址(asmx文件地址)

_Namespace:webservice的命名空间

<script language="javascript" type="text/javascript">
// <!CDATA[

//define
var xmlhttp;
var value=new Array();
var variable=new Array();

//Show Response MSG.
function handleStateChange()
{
  var h=document.getElementById("Label1");
  if(xmlhttp.readyState==4)
  {
    if(xmlhttp.status==200)
    {
      alert(xmlhttp.responseText);
      h.innerHTML=xmlhttp.responseText;
      //h.innerHTML=xmlhttp.responseXML;
    }
    else if(xmlhttp.status==404)
    {
      h.innerHTML="<br>找不到请求的服务器资源!";
    }
  }
  else if(xmlhttp.readyState==0)
  {
    h.innerHTML="<br>未初始化!";
  }
  else if(xmlhttp.readyState==1)
  {
    h.innerHTML="<br>正在加载……!";
  }
  else if(xmlhttp.readyState==2)
  {
    h.innerHTML="<br>已经加载完成!";
  }
  else if(xmlhttp.readyState==3)
  {
    h.innerHTML="<br>正在和服务器交互";
  }
  else
  {
    h.innerHTML=xmlhttp.responseXML;
  }

}

//Get Request Data's length
function getlen(str)
{
var bytesCount=0;
for (var i = 0; i < str.length; i++)
{
var c = str.charAt(i);
if (/^[\u0000-\u00ff]$/.test(c)) //匹配双字节
   {
bytesCount += 1;
}
else
{
bytesCount += 2;
}
}
return bytesCount;

}

//Create XMLHttpRequest Object
 function createXMLHttpRequest()
{

  if(window.ActiveXObject)
  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequst)
  {
    xmlhttp=new XMLHttpRequest();
  }
}

//send Request By HTTP POST

function RequestByPost(method,variable,value,url,_Namespace)
{
createXMLHttpRequest();
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope 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/">';
data = data + '<soap:Body>';
data = data + '<'+method+' xmlns="'+_Namespace+'">';
for(var i=0;i<variable.length;i++)
{
  data = data + '<'+variable[i]+'>'+value[i]+'</'+variable[i]+'>';

}
data = data + '</'+method+'>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';

xmlhttp.onreadystatechange=handleStateChange;
xmlhttp.Open("POST",url, true);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("Content-Length",getlen(data));
xmlhttp.SetRequestHeader ("SOAPAction",_Namespace+method);
xmlhttp.Send(data);
alert(data);
}

//CallHelloWorld!
function SayHello_onclick() {
//alert(document.getElementById('YourName').value);
 RequestByPost("HelloWorld",new Array("msg"),new Array(document.getElementById('YourName').value),"WebService.asmx","localhost/");
}
//WeatherReport Test:
function Button2_onclick() {
RequestByPost("getWeatherbyCityName",new Array("theCityName"),new Array(document.getElementById('CityName').value),"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx","http://WebXml.com.cn/");
}

// ]]>
</script>
////////////////////////////////
////////Sixi. Let it be.../////
//////////////////////////////

时间: 2024-05-03 05:19:45

JS调用webservice的通用函数的相关文章

js调用webservice构造SOAP进行身份验证_javascript技巧

本文实例为大家分享了js调用webservice构造SOAP进行身份验证的相关内容,注释清除,供大家参考,具体内容如下 <html> <head> <title>无标题页</title> <script language="javascript" type="text/javascript"> // <!CDATA[ //define var xmlhttp; var value=new Array(

JS调用WebService示例

要想实现JS调用WebService,可以按如下步骤实现(经过测试): 第一步:创建一个WebService   在此处我就创建了一个默认的web服务,并不做什么修改,把主要集力放在怎么实现JS调Web服务上面. 代码 using System;using System.Collections.Generic;using System.Web;using System.Web.Services; /// <summary>///WebService 的摘要说明/// </summary&

js调用WebService

js|web <!--    //使用方法,直接调用sendRequest(url, Method, HttpMethod, params)方法    //参数说明:url--访问地址:Method--调用的服务方法:HttpMethod--传递方法,默认post:params--需要传递的参数或信息:    //多参数使用&连接,post方法经过测试:    //注意!    //成功传输回响函数为xmlResponse(),请自行在此js引用之前添加,否则无效,回响内容为req.resp

js调用webservice中的方法实现思路及代码_javascript技巧

webservice代码: 复制代码 代码如下: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Services; using StoreHouse.Common; using StoreHouse.IDAL; using StoreHouse.Mode

【急救】js使用webservice.htc外网访问被拒绝的问题

问题描述 我用webservice.htc通过js调用webservice,在局域网内使用http://ip/***.asmx?wsdl,调用可以成功但是挂到公网上之后,用http://[网址]/***.asmx?wsdl,结果返回错误,错误信息为"Accessdenied".请问是怎么回事啊? 解决方案 解决方案二:权限不够解决方案三:应该是没有权限解决方案四:追加iis用户权限解决方案五:IIS下的目录安全性中勾选允许匿名访问解决方案六:不是权限不够吧,如果权限不够,挂起来之后在局

javascript调用WebService - Hello World

WebService既可以大用,也可以小用.大用可以应用到整个系统,将整个系统设计成SOA(面向服务),小用可以应用到JS.今天我们就来看一下如何在网页中用JS来调用WebService. 我们举的例子很简单,用 JS调用默认VS2008生成的Hello World服务.Service的代码如下: using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Lin

C#调用WebService示例

C#调用WebService时和JS调用时有很多相似的地方,让我们来一步一步实现: 第一步:创建一个WebService 这一步和JS调用WebService相同,在此不多说了.详细可访问如下地址: 地址:http://www.cnblogs.com/puresoul/archive/2010/08/19/1803567.html 第二步:创建一个页面,实现C#调用Web服务    在页面上添加一个按钮,后台代码如下: 代码 1 using System; 2 using System.Coll

JS调用PageMethods(转)

操作步骤: 1.新建一个WebApplication项目,取默认设置. 2.双击设计界面,进入后天代码编辑界面,代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 using System; using System.Web.Services;   namespace WebApplication1 {     public partial class _Default : System.Web.UI.Page

js跨域调用WebService的简单实例_AJAX相关

步骤1.   在web.config中的system.web节点里加入 <!--此节点可允许脚本跨域调用webservice--> <webServices> <protocols> <add name="HttpPost"/> <add name="HttpGet"/> </protocols> </webServices> <!--此节点可允许脚本跨域调用webservic