jeudi 5 janvier 2017

Preflight response does not contain the Access-Control-Allow-Origin header

I am trying to consume a SOAP web service which locates IP addresses (I do not own the server) and I'm getting the following error whenever I pass any IP address:

XMLHttpRequest cannot load http://ift.tt/2j9NMms. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:58356' is therefore not allowed access.

If I type '74.125.224.72' (Google's IP address) and click the button, the xml respose should look like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://ift.tt/sVJIaE" xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
   <soap:Body>
     <GetGeoIPResponse xmlns="http://ift.tt/18TcHUI">      
       <GetGeoIPResult>
         <ReturnCode>1</ReturnCode>
         <IP>74.125.224.72</IP>          
         <ReturnCodeDetails>Success</ReturnCodeDetails>
         <CountryName>United States</CountryName>
         <CountryCode>USA</CountryCode>
       </GetGeoIPResult>
    </GetGeoIPResponse>
  </soap:Body>
</soap:Envelope>

What can I do to solve the problem?

Here is the code:

<!Doctype html>

<head>
    <title>Calling Web Service from jQuery</title>

    <script type="text/javascript" src="http://ift.tt/1kJlpH7"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
                var wsUrl = "http://ift.tt/2j9NMms";

                var IPAddress = $('#txtIPAddress').val();
                // 74.125.224.72

                var soapRequest =
                    '<soapenv:Envelope xmlns:soapenv="http://ift.tt/sVJIaE" xmlns:web="http://ift.tt/18TcHUI">' +
                    '<soapenv:Header/>' +
                    '<soapenv:Body>' +
                    '<web:GetGeoIP>' +
                    '<web:IPAddress>' + IPAddress + '</web:IPAddress>' +
                    '</web:GetGeoIP>' +
                    '</soapenv:Body>' +
                    '</soapenv:Envelope>';

                $.ajax({
                    type: "POST",
                    url: wsUrl,
                    crossDomain: true,
                    contentType: "text/xml",
                    dataType: "xml",
                    data: soapRequest,
                    success: processSuccess,
                    error: processError
                });

                $('#txtIPAddress').val('');

            });
        });



        function processSuccess(data, status, req) {
            if (status == "success")
                $("#response").text($(req.responseXML).find("HelloResult").text());
        }

        function processError(data, status, req) {
            alert(req.responseText + " " + status);
        }
    </script>

</head>

<body>
    <h3>
        Calling Web Service with jQuery/AJAX
    </h3> Enter IP address:
    <input id="txtIPAddress" type="text" />
    <input id="btnCallWebService" value="Call web service" type="button" />
    <div id="response"></div>
</body>

</html>



Aucun commentaire:

Enregistrer un commentaire