views:

75

answers:

2

Hi guys, I am trying to use SOAP in javascript but i am not getting how to start it. Here is the code sample i write in PHP and it works fine. I want to write this code in Javascript. In following code i call one api from a www.example.com and for calling certain api we require to pass some parameters.

    $soapClient = new SoapClient("https://www.example.com/WSDL");     
    $param_sh = array( ); 
    $header = new SoapHeader('http://somesite.com/action/', 'user_credential', $param_sh); 
    $soapClient->__setSoapHeaders(array($header)) 
    $param = array("with some parameter");
    $contents = $soapClient->__call("name_of_method",array($param)); 

    print($contents);  

Thanx in advance!!!

+1  A: 

Assuming you're talking about in-browser Javascript, you won't be able to use SOAP. Browsers obey something called the Same Origin Policy, which says (loosely) that you can't make cross domain requests from javascript. That means you can only make requests to a SOAP service (or any HTTP request) if it's on the same domain that the browser page is currently on. Because of this limitation/feature, no one ever went to a lot of trouble to implement a SOAP client in Javascript (although they probably do exist).

Your best bet is to make your SOAP calls via PHP, and then make AJAX requests from your web page (via javascript) to the PHP pages that make the actual SOAP request.

Alan Storm
Although more modern browsers do allow cross-domain requests, using appropriate headers: see http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ , http://www.w3.org/TR/access-control/
Matthew Wilson
+1  A: 

JavaScript doesn't have a SOAP library out of the box, though you can google around and find them, e.g., here. Not that this won't do anything to work around the limitations @Alan cites. The web services still need to be on the origin server.

Marcelo Cantos