开发者

Call Webservice using Javascript

I am trying to call a webservice using javascript.But it shows an error like selectSingleNode() is not a method.I am trying it in mozilla firefox.Which is perfectly working in explorer when i change XMLHttpRequest to ActiveXObject.here i am adding my source code which i am tried i开发者_Go百科n firefox.

<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"/>

Please help me...Already which kill my 8 more hours...


i would suggest using the jquery library. it has some pretty slick mechanisms for get, put and ajax calls. it will work in all the browsers and if there are lots of tutorials and support forums to help you work out your problems


First, You need to use a cross browser technique to ensure you get a valid XMLHttpRequest object.

Not only will this method give you the best XHR for the browser, it is a 'memoizing' function. This means that the factory logic is only executed once.

And you can learn more about calling various types of services in JS here.

function createXHR() {
    var xmlhttp, XMLHttpFactories = [
        function() {
            return new XMLHttpRequest();
        }, function() {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }, function() {
            return new ActiveXObject("Msxml3.XMLHTTP");
        }, function() {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    ];
    for (var i = 0; i < XMLHttpFactories.length; i++) {
        try {
            xmlhttp = XMLHttpFactories[i]();
            this.createXHR = XMLHttpFactories[i];
            return xmlhttp;
        } catch (e) { }
    }
}

Second, you need to use a crossbrowser technique for using xml. You can learn from http://www.w3schools.com/Xml/xml_parser.asp

if (window.DOMParser)
  {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(text,"text/xml");
  }
else // Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(text); 
  }

OR

if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // Internet Explorer 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","books.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;


I would suggest to use JSON for this. It is very simple and the browser compatibilities are taken care by the JSON scrypting.

It is just like to call a webservice with proxy, calling a public function and passing the values if any. This code is only for .NET.

Create a webservice and dont forget to add the ScriptService tab.

// Summary description for RScriptService /// </summary>
[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService()] public class ProfileService : System.Web.Services.WebService {Public string ExecuteCommand(param, param,param){}

}

Add a script entry in your aspx file

<cc1:ToolkitScriptManager ID="ScriptManager1" AsyncPostBackTimeout="600" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError"EnablePageMethods="true"><Services><asp:ServiceReference Path="ProfileService.asmx"/></Services></cc1:ToolkitScriptManager>

Call these webservice using Javascript code.

//Call the function function GetProfileDetails(){ProfileService.ExecuteCommand(request1,request2, OnGetProfileDetailsSuccess, OnGetProfileDetailsError);}

//Call back for succes function OnGetProfileDetailsSuccess(result){for (var property in result) {//get the result result[property];}}

//Call back for error function OnGetProfileDetailsError(error) {alert("An error occured while executing command<br/>" + error.get_message());}

Hope this helps..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜