使用JavaScript使用Web服务(返回dataSet)

时间:2012-09-24 12:52:38

标签: javascript web-services ado.net dataset

我使用Javascript使用以下代码使用Web Service .NET:

// This is where you can place your Javascript code
// var xmlHTTP;

function MButton1Click(event) {
testarTexto();
}


function WebSvc()   // Class Signature
   {
       WebSvc.prototype.CallWebService = function(url, soapXml, callback)
       {

           var xmlDoc = null;

           if (window.XMLHttpRequest)
           {
               xmlDoc = new XMLHttpRequest(); //Newer browsers
           }
           else if (window.ActiveXObject) //IE 5, 6
           {
               xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
           }

           if (xmlDoc)
           {
               var self = this;
               xmlDoc.onreadystatechange = function() { self.StateChange(xmlDoc, callback); };

               xmlDoc.open("POST", url, true);
               xmlDoc.setRequestHeader("Content-Type", "text/xml");
               xmlDoc.setRequestHeader("Content-Length", soapXml.length);
               xmlDoc.send(soapXml);
           }
    else
           {
               if (callback)
               {
                   callback(false, "unable to create XMLHttpRequest object");
               }
           }
       };

       WebSvc.prototype.StateChange = function(xmlDoc, callback)
       {
 if (xmlDoc.readyState === 4)
           {
               var text = "";

               if (xmlDoc.status === 200)
               {
                   text = xmlDoc.responseText;
               }

               // Perform callback with data if callback function signature was provided,
               if (callback !== null)
               {
                   callback(xmlDoc.status === 200, text);
               }
           }
       };
   }


   function createSoapHeader(message)
   {
        var soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
   soap = soap + "<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/\">";
   soap = soap + "  <soap:Body>";
   soap = soap + "    <TesteDeTexto xmlns=\"http://tempuri.org/\">";
   soap = soap + "      <pTexto>" + message + "</pTexto>";
   soap = soap + "    </TesteDeTexto>";
   soap = soap + "  </soap:Body>";
   soap = soap + "</soap:Envelope>";
       return soap;
   }

   function callComplete(result, data)
   {
       if (result)
       {
         //alert(getTagValue(data, "TesteDeTextoResult"));
           document.getElementById('MTextArea1').value = getTagValue(data, "TesteDeTextoResult");
       }
       else
       {
           alert("Error occurred calling web service.\n" + result + "\n" +data);
       }
   }
   function testarTexto()
   {

       var soap = createSoapHeader("TESTE");
       //document.getElementById('texto').value = soap;
//     alert(soap);

       var webServiceCall = new WebSvc();

       webServiceCall.CallWebService("http://localhost/WebService/Test.asmx", soap, callComplete);
   }

   function getTagValue(inputStr, tagName)
   {

       var stag = "<" + tagName + ">";
       var etag = "<" + "/" + tagName + ">";

       var startPos = inputStr.indexOf(stag, 0);
       if (startPos >= 0)
       {
           var endPos = inputStr.indexOf(etag, startPos);
           if (endPos > startPos)
           {
               startPos = startPos + stag.length;
               return inputStr.substring(startPos, endPos);
           }
       }

       return "";
   }

但它只适用于返回字符串作为结果的方法。当我需要返回dataSet时如何制作?

1 个答案:

答案 0 :(得分:0)

使用jQuery ^^在那里你可以找到很多不同的返回类型的处理程序。 dataSet可以用JSON或XML编码,通过jQuery自动转换为JS数据结构。另外,JS代码看起来更清晰,因为jQuery带有一堆AJAX methods。要将xml响应转换为JS数据结构,只需在jQuery请求中将dataType设置为xml即可。例如:

jQuery.ajax('url/to/your/WS', {
   dataType: "xml",
   data: {
      postParam1 : "foo"
   },
   success: function(data) {
      alert(data.find("MyNode"));
   }
});

如何在JS中使用解析的xml响应here