How to use soap in javascript

做~自己de王妃 提交于 2019-12-24 10:58:22

问题


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);  

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2577237/how-to-use-soap-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!