Javascrpit调用webservice接口(soap方式)

来源:互联网 发布:查看数据库表空间 编辑:程序博客网 时间:2024/04/29 08:23

后台web服务:在.net里搭建web服务,发布webservice成功后,输入自己设定的网址即可出现如下页面。



webservice代码:

using System.Web;
using System.Web.Services;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using System.Net.WebSockets;

namespace webservice
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </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 WebService1 : System.Web.Services.WebService
    {

   [WebMethod]   //在写函数前面要用[WebMethod]标记,表明是方法
        public string Name( )
    {

        return "myname is lily";   //无参数时的返回
    }
        [WebMethod]
        
        public int  add(int num1,int  num2)
        {
            return num1+num2;  //有参数时的返回
        }
        
    }


js代码:

(1)有参数时

<script type="text/javascript">
var URL = "http://localhost:8888";  //这是我自己在发布web服务时设定的地址
function  add() {    //开始拼接,参考soap1.2请求,按规范拼接请求    var data;      data = '<?xml version="1.0" encoding="utf-8"?>';    data = data + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';    data = data + '<soap12:Body>';    data = data + '<add xmlns="http://tempuri.org/">';   //add为函数名,有参数时不能再此处写结束标签    data=data+'<num1 >2</num1>';                          //num1、num2为参数名    data=data+'<num2>3</num2>';    data=data+'</add>';                                  //函数名的结束标签                                 data = data + '</soap12:Body>';    data = data + '</soap12:Envelope>';    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  //IE    xmlhttp.Open("POST", URL, false);    xmlhttp.SetRequestHeader("Content-Type", "application/soap+xml");      xmlhttp.Send(data);    document.getElementById("data1").innerHTML=xmlhttp.responseText; 
}
</script>
(2)无参数时
<script type="text/javascript">
var URL = "http://localhost:8888";  //这是我自己在发布web服务时设定的地址

  function  getname() {        //拼接        var data;        data = '<?xml version="1.0" encoding="utf-8"?>';        data = data + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';        data = data + '<soap12:Body>';        data = data + '<Name xmlns="http://tempuri.org/"/>';  //Name为函数名,注意无参数时此处有结束标签        data = data + '</soap12:Body>';        data = data + '</soap12:Envelope>';        //创建异步对象        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  //IE        xmlhttp.Open("POST", URL, false);        xmlhttp.SetRequestHeader("Content-Type", "application/soap+xml");        xmlhttp.Send(data);        alert(xmlhttp.responseText);        document.getElementById("data2").innerHTML=xmlhttp.responseText;    }</script>
(3)html部分
<body><form id="form1" runat="server">    <div>        <button id="one" type="button"  onclick="add()"> 求和 </button>    </div>    <div id="data1">    </div>    <div>        <button id="two" type="button"  onclick="getname()"> 姓名 </button>    </div>    <div id="data2">    </div></form></body>
结果显示:


原创粉丝点击