官网 帮助中心 社区 下载本系统 学习
热词:低代码发布
JS访问webServices

发布: EKETEAM    最后编辑时间: 2023-11-21 23:12    浏览: 176    

(1)使用ActiveXObject

    function requestByGet(){   
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
        //Webservice location.  
        var URL="http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=chengdu&CountryName=china";  
        xmlhttp.Open("GET",URL, false);   
        xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");   
        xmlhttp.SetRequestHeader ("SOAPAction","http://http://www.webserviceX.NET/GetWeather");   
        xmlhttp.Send(null);   
        var result = xmlhttp.status;   
        //OK  
        if(result==200) {   
            alert(xmlhttp.responseText);   
        }   
        xmlhttp = null;   
    }


    function requestByPost(city,country){  
        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 + '<GetWeather xmlns="http://www.webserviceX.NET">';   
        data = data + '<CityName>'+city+'</CityName>';   
        data = data + '<CountryName>'+country+'</CountryName>';  
        data = data + '</GetWeather>';   
        data = data + '</soap:Body>';   
        data = data + '</soap:Envelope>';   
      
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
        var URL="http://www.webservicex.net/globalweather.asmx";  
        xmlhttp.Open("POST",URL, false);   
        xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");   
        xmlhttp.SetRequestHeader ("SOAPAction","http://www.webserviceX.NET/GetWeather");   
        xmlhttp.Send(data);   
        alert( xmlhttp.responseText);   
    }

    (2)使用jQuery的ajax
    $(function(){  
         $.ajax({  
            type: "GET",//GET or POST  
            url: "http://www.webservicex.net/globalweather.asmx/GetWeather", ////Webservice location  
            data: {CityName: 'chengdu', CountryName: 'china'},  
            dataType: "xml",  
              
            success: function(data, textStatus){  
                var xml = data.xml.replace(/&lt;/g,"<").replace(/&gt;/g,">");  
                alert(xml);  
                //alert($(xml).find("Status").length);  
      
                alert($(xml).find("Temperature").eq(0).text());  
                $(xml).find("Status").each(function(i,obj){  
                    //alert(obj.innerText);  
                    alert($(this).text());  
                });  
            },  
      
            error:function(XMLHttpRequest, textStatus, errorThrown){  
                alert(textStatus);  
            }  
         });          
      
    });

以上信息是否对您有帮助?