1

Can i Call web service from java script?

Thanks

1
  • I think that depends on the server you are trying to call. If it supports then you can. Commented Nov 20, 2009 at 10:47

4 Answers 4

2

Yes, you can do this.

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

2 Comments

What kind of service do you want to call? Please be more specific.
i want to call service that return array and that array i use in java script
1

Definitely. We would need a bit more information to know what kind of service you are using and if you are using a JS library. This is very easy with Dojo or EXT. I'll show you a Dojo example as that is what I'm working with the most lately. I mostly create my services as REST services at this point. Depending on the service and how it's going to be used, I either send the response back as JSON or JSONP. Below is an example for services that send the response as JSONP, which I use for cross-domain calls. You would need to use dojo.io.script.get (if using the Dojo library):

dojo.io.script.get({
    callbackParamName: 'method',
    url: 'http://mydomain/myservicename/mymethodname/param1/param2',
    timeout: 20000,
    load: dojo.hitch(this,function(response,ioArgs) {
        this.doSomething(response);
    }),
    error: dojo.hitch(this,function(error) {
        alert('uh oh, something went wrong');
    })
});

For services that send the response back as JSON, you can use the following Dojo functions: dojo.xhr, dojo.xhrDelete, dojo.xhrGet, dojo.xhrPost, dojo.xhrPut, dojo.rawXhrPost, and dojo.rawXhrPut depending on the type of call you make. Below is an example:

dojo.rawXhrPost({
    url: url,
    handleAs: 'json',
    postData: parametersJSON,
    headers: { "Content-Type": "text/json" },
    timeout: 45000,
    //function to be run in case of successful call to the specified Web method
    load: function(data) {
        onComplete(data);
    },
    //function to be run in case of failed call to the specified Web method
    error: function(error) {
        onError(error.message);
    }
});

Comments

1

You can call a webservice on the same server as the page with a normal XHR call. If the server is on a different server then you should use a JSONP call. NOTE the JSONP doesnt have the best error handling.

Comments

1

You can easily call a JSON or a RESTful web service.

For SOAP web services you need a library.

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.