我想調用一個web服務使用javascript.But它顯示一個錯誤,如 selectSingleNode()不是一個方法。我試圖在Mozilla Firefox中。當我更改XMLHttpRequest到ActiveXObject.here我添加我的源代碼,我在Firefox中嘗試。使用Javascript調用Webservice
<script language="javascript">
// Web Service functionality
// Global vars
var xmlDoc = null;
var _serviceCallback = null;
// Calls web service, web service url and parms, and callback function or null must be provided.
// Callback function receives a true or false based on success of call to host
function callWebService(url, callback)
{
_serviceCallback = callback;
if(xmlDoc == null)
{
// xmlDoc = new XMLHttpRequest();
xmlDoc = new XMLHttpRequest();
}
xmlDoc.onreadystatechange = stateChange; //callback for readystate
xmlDoc.async = true; //do background processing
//xmlDoc.load(url);
xmlDoc.open('GET', url);
xmlDoc.send();
//var doc= xmlDoc.responseXML;
}
// Updates readystate by callback
function stateChange()
{
if (xmlDoc.readyState == 4)
{
var err = xmlDoc.parseError;
var result = false;
var nd;
if(err.errorCode == 0)
{
nd = xmlDoc.selectSingleNode("//envelope/date_time");
if(nd.text != "")
result = true;
}
// perform callback if provided
if(_serviceCallback != null)
_serviceCallback(result, nd == null ? "" : nd.text);
}
}
// Callback supplied to XMLHttpRequest call
function callbackTest(result, data)
{
obj = document.getElementById("txtOuput");
if(result)
obj.value = "Success " + data;
else
obj.value = "Web Service Call Failed";
}
</script>
<input type="button" onclick="callWebService('http://www.hendricksongroup.com/services/WebService.asmx/GetTime?input=Test', callbackTest)" value="Click" />
<input type="text" id="txtOuput"/>
請幫我...這已經殺了我8個小時......
它會更容易幫助,如果你把你的代碼中的一個代碼塊,以使其更具可讀性... – sblom 2010-03-31 04:02:50
是否有你的理由阻止使用已建立並經過測試的AJAX庫(如jQuery)? – 2010-03-31 04:05:24
儘管大多數指向jquery等的非答案答案都是有意的,但理解這些東西如何工作是有價值的。這個問題很簡單,答案也是如此。乾杯。 – 2010-03-31 04:35:09