How to use soap in javascript
I am trying to use SOAP in javascript but i am not getting how to s开发者_如何学Gotart 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);
Now, after more than 5 years since this question was asked, lots of stuff has happened to Javascript and web in general, so:
you can send cross-origin requests and modern browsers are 100% OK with that (if the server you're sending them to allows CORS, of course)
there are lots of libraries both for server and client side javascript.
there are lots of questions on the topic on StackOverflow.
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.
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.
精彩评论