类别:程序开发

日期:2023-06-18 浏览:1461 评论:0

一、调用

第一种方式:

	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 + '<GetOpenId xmlns="http://tempuri.org/" >';
	data = data + '<cell>1</cell>';
	data = data + '</GetOpenId>';
	data = data + '</soap12:Body>';
	data = data + '</soap12:Envelope>';
	var json;
	var xmlhttp = new XMLHttpRequest();
	// var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open("post", "http:/localhost/InterFaceService.asmx", false);
	xmlhttp.setRequestHeader("Content-Type", "application/soap+xml");
	//     xmlhttp.setRequestHeader("Content-Security-Policy", "upgrade-insecure-requests");
	xmlhttp.onreadystatechange = function() { //绑定响应状态事件监听函数     
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                                          
			var info = xmlhttp.responseText;
			//显示JSON对象字符串
			console.log( xmlhttp.responseText);
			var me='GetOpenId'+'Result';
	    	var response=xmlhttp.responseXML.documentElement;
		  var method=response.getElementsByTagName(me)[0].firstChild.data; 
		 alert(method);
		}
	}
	try {
		xmlhttp.send(data);
	} catch (e) {
		console.log(e);
	}

第二种方式:

 $("#btnHello").click(function () {
            alert(111);
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"http://www.hnsxcsjayy.com/API/MessageService.asmx/HelloMessage",
                //data:"{tzType:2,openlD:'oFUCGt666FVHxWDheDRsSnWqU3YU',type:2,url:'',first:'1',keyword1:'2',keyword2:'3',keyword3:'4',keyword4:'5',keyword5:'6',remark:'7'}",
                data:"{tzType:2,openID:'oFUCGt666FVHxWDheDRsSnWqU3YU',type:2,url:'',first:'1',keyword1:'2',keyword2:'3',keyword3:'4',keyword4:'5',keyword5:'6',remark:'7'}",
                dataType:'json',
                success:function(result){                   
                    alert(result.d);
                }
            });
        });

二、跨域

浏览器针对js默认做了跨域认证,要进行跨域使用,就必须对webservice的web.config进行取消跨域配置。
注:如果一方是http,则webservice 也必须是http。 同理https也是一样。
web.config完整代码:

configuration>
  <system.web>
<webServices>
       <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
        <add name="HttpSoap"/>
        <add name="Documentation"/>
       </protocols>
    </webServices>
  </system.web>
    <system.webServer>
 <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
        <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Request-Private-Network" value="true" />
      </customHeaders>
    </httpProtocol>
    </system.webServer>
</configuration>

三、返回值解析

var me='GetOpenId'+'Result';//GetOpenId 是请求webservice的方法,Result是固定的字符。GetOpenId可根据请求的方法定义变量,从而实现动态获取
var response=xmlhttp.responseXML.documentElement;
var method=response.getElementsByTagName(me)[0].firstChild.data;


本文标题:Js调用webservice,如何调用,跨域问题,返回值解析。
本文链接:https://vtzw.com/post/1093.html
作者授权:除特别说明外,本文由 零一 原创编译并授权 零一的世界 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
 您阅读本篇文章共花了: 

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。