0

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 in 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...

3
  • It'd be easier to help if you put your code in a code block so as to make it more readable... Commented Mar 31, 2010 at 4:02
  • 2
    Is there a reason you're prevented from using an established and battle tested AJAX library such as jQuery? Commented Mar 31, 2010 at 4:05
  • 3
    While most of the non-answer answers pointing to jquery etc are well intentioned, there is value in understanding how these things work. This question is simple as is the answer. Cheers. Commented Mar 31, 2010 at 4:35

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

5 Comments

And duplication functionality of existing JS frameworks is the right answer?
@R0MANARMY - there is no mention of any framework in the question. This is the answer to the question.
Technically you're right, there's no mention of any frameworks and your answer is the answer to the question. However I think there's value in pointing the OP in the direction of a framework in case s/he wasn't aware/familiar with them.
@R0MANARMY - that is what comments are for. Relax, I ain't mad at ya, just think your response to this particular question sucks. ;-)
I know it's not personal, I just thought back to all the times people answered the question I asked instead of answering the question I should have asked =). You're right about using comments though, I'll delete my answer.
0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.