次のコードで Javascript を使用して WebService .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 を返す必要がある場合はどうすればよいですか?