0

I am new to AngularJS and SOAP and I am doing a small application that performs a soap call to public APIs. The problem is that I get this error: No "Access-Control-Allow-Origin" on headeris present. I've searched a lot, and I have found out that this error is related to CORS. How can I solve it? I'm using Chrome and I found a plugin that allows it and now I get error 500, so I don't know if I resolved it or if it is something else. Here's my code.

<!DOCTYPE html>
<html>
<head>
    <title>Chiamata SOAP</title>
    <script src="lib/angular.min.js"></script>
    <script src="lib/soapclient.js"></script>
    <script src="lib/angular.soap.js"></script>
    <script>
        var myApp = angular.module('myApp', ['angularSoap'])
        .factory("testService", ['$soap',function($soap) {
        var base_url = "http://www.webservicex.net/globalweather.asmx?wsdl";

            //$soap.setCredentials("admin", "admin");

            return{
                GetCitiesByCountry: function(){
                    return $soap.post(base_url, "GetCitiesByCountry ", {CountryName:"Italy"});
                }
            }
        }])
        .controller('soapCtrl', function($scope, testService){
            $scope.onClickCall = function(){
                    console.log("text before call")
                    testService.GetCitiesByCountry().then(function(response){
                        console.log("Testo dopo la chiamata")
                        console.log(response);
                        $scope.data = response.data;
                    });
            }
        });
    </script>
</head>
<body ng-app="myApp">
<div ng-controller="soapCtrl">
    <span>Effettua la chiamata SOAP </span><button ng-click="onClickCall()">Invia</button>
</div>

</body>
</html>
Setily
  • 814
  • 1
  • 9
  • 21
Wallcraft
  • 21
  • 1
  • 8
  • CORS issues are usually rectified on the server side - I wont say "fixed", because it may be that the server doesn't want your page to access the resources, so the CORS "block" is not a problem as far as they are cioncerned – Jaromanda X Aug 30 '16 at 11:11
  • "i found a plugin that allow it" — Those plugins are dirty hacks and generally only work with simple requests (which SOAP is not) – Quentin Aug 30 '16 at 11:17
  • Ah okay, so what i need to do for resolver this issue? – Wallcraft Aug 30 '16 at 11:21

1 Answers1

0

I've searched a lot, and i find that this error is related to CORS.

Only tangentially. The error is related to the Same Origin Policy, whereas CORS is a mechanism by which the server you're talking to can relax the same origin policy for a given client (e.g., the origin of the page making the ajax request).

If your page's origin isn't allowed to make that request to the server, there's nothing you can do in your client-side code that will make the server allow the request.

If you control the server, look at the CORS spec and the umpteen how-to's for CORS to find out how to implement the reply that allows your page's origin to query it.

If you don't control the server:

  • Look at its documentation and/or contact the people who do. Perhaps it has a JSONP alternative to SOAP (JSONP is another way to work around the SOP, but again requires the server to implement it).
  • Set up your own server and make the call from your client to your server and have your server query the other one. The SOP only applies to requests on browsers, not server-to-server.
  • The other usual recommendation at this stage would be to look at scraping the information via the Yahoo! Query Language, but that doesn't apply to a SOAP request.
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875