预检响应不包含Access-Control-Allow-Origin标头

时间:2017-01-06 00:42:00

标签: web-services web soap http-headers cors

我正在尝试使用定位IP地址的SOAP Web服务(我不拥有服务器),每当我传递任何IP地址时都会收到以下错误:

  

XMLHttpRequest无法加载http://www.webservicex.net/geoipservice.asmx?WSDL/。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://127.0.0.1:58356”访问。

如果我输入'74 .125.224.72'(Google的IP地址)并单击按钮,则xml respose应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
     <GetGeoIPResponse xmlns="http://www.webservicex.net/">      
       <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>

我该怎么做才能解决问题?

以下是代码:

<!Doctype html>

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

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
                var wsUrl = "http://www.webservicex.net/geoipservice.asmx?WSDL/";

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

                var soapRequest =
                    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webservicex.net/">' +
                    '<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>

0 个答案:

没有答案