1

I want to call a web service of which URL is available to me. I want to just call it and retrieve the result back using javascript Ajax. For example : if some web service of like addition of two numbers is available freely and I want to use it in my app, how should I start ? I have just implemented following code (don't know it is right or not):

function webServiceCallResult(){    
    var xmlHttpReq = getXMLHttpRequest();
      if(xmlHttpReq == null){
        alert('Exception occurred');
        return false;
    }    
    var strURL = "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml";
    //var strURL = 'http://w3schools.com/dom/note.xml';
    if(xmlHttpReq.readyState == 0){
        xmlHttpReq.open('GET', strURL, true);
        xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4) {
                var resultString = xmlHttpReq.responseXML;              
                document.getElementById('webserviceresponsetext').value = resultString.getElementsByTagName("website")[0].childNodes[0].nodeValue;  
            }
        }
        xmlHttpReq.send();
    }
}

This is working fine in IE but giving error in FF, Opera etc.like

XML parsing error,no element found Location: moz-nullprincipal:{ce91453b-f84c-4ce8-b02c-b999ef9f013a} Line Number 1, Column 1

Is it even possible to call a web service without using SOAP service request ? Thanks..

3 Answers 3

1

Unless you want to deal with the browser specific cross-compatibility stuff you should be using a library to handle Ajax requests.

Also I saw you are making an Ajax request to a different server. Unfortunately the same origin policy prevents cross domain XHR, but there is a workaround about this, by using JSONP.

I recommend using jQuery's Ajax method with JSONP, read some articles about this here: http://remysharp.com/2007/10/08/what-is-jsonp/
http://www.giantflyingsaucer.com/blog/?p=2682

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

Comments

0

AFAIK the same origin policy prevents cross domain XHR. You'll have to host a service on your server side which talks to the remote endpoint.

Comments

0

Are you firing the request from same domain?

Firefox won't allow you to use XMLHttpRequest to access a different domain. See the link below.

http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php

You should call the request from the pages under http://www.androidpeople.com/ only.

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.