javascript - 无法从网络服务获取响应

标签 javascript jquery html

我需要验证以下网站的增值税 ID http://ec.europa.eu/taxation_customs/vies/vatRequest.html 我正在使用 bwlow 代码访问上述网站的网络服务,但没有用

<html>
<head>
    <title></title>
    <script src="jquery-1.8.2.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            $("#BTNSERVICE").click(function (event) {
                var webserUrl = "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl";
                var soapRequest =
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" \
xmlns:urn="urn:ec.europa.eu:taxud:vies:services:checkVat:types">\
    <soapenv:Header/>\
    <soapenv:Body>\
       <urn:checkVat>\
          <urn:countryCode>MS</urn:countryCode>\
          <urn:vatNumber>TESTVATNUMBER</urn:vatNumber>\
       </urn:checkVat>\
    </soapenv:Body>\
 </soapenv:Envelope>';
                $.ajax({
                    type: "POST",
                    url: webserUrl,
                    contentType: "text/xml",
                    dataType: "xml",
                    data: soapRequest,
                    success: SuccessOccur,
                    error: ErrorOccur

                });
            });
        });
        function SuccessOccur(data,status, req) {

        alert(status);
            if (status == "success")
            {

                alert('sucess');
                alert(req.responseText);
                }
        }
        function ErrorOccur(data,status, req) {
        alert(status);
            alert(req.responseText + " " + status);
        }
    </script>
</head>
<body>
    <form runat="server">
    <button id="BTNSERVICE" runat="server" text="BTNSERVICE" />
    SAMPLE Application to test service
    </form>
</body>
</html>  

运行上面的代码后,它实际上应该抛出“否,无效输出”,但它抛出类似“未定义错误”的错误。不确定出了什么问题。

注意:我在上面的代码中发送的 SOAP 请求在下面的网站中提供 http://ec.europa.eu/taxation_customs/vies/vatRequest.html 谁能帮助我哪里出错了? 提前致谢...

最佳答案

所以,正如我在评论中所说,问题出在同源策略中。请求的资源上不存在“Access-Control-Allow-Origin” header ,因此您无法直接从浏览器进行此调用,除非禁用 CORS(但您不会在生产中执行此操作)。

您需要做的是构建一个轻量级服务,通过它您可以路由您的请求。您的服务将调用 SOAP VAT Web 服务并传回结果。您甚至可以利用这个机会在服务器端执行一些逻辑并进一步简化客户端 JavaScript 调用。

如果您在服务器上使用 JavaScript/NodeJs 解决方案,我建议使用 SOAP 库,而不是自己构建信封。查看:http://www.codeproject.com/Articles/12816/JavaScript-SOAP-Client

或者,如果您确实想在 JS 中构建自己的: http://www.ibm.com/developerworks/webservices/library/ws-wsajax/

关于javascript - 无法从网络服务获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32122017/

相关文章:

javascript - 通过javascript/html访问twitter share iframe

jquery - 如何根据内容量动态更改div的大小

html - IE 7 问题 HTML/CSS

java - 如何只获取HTML页面的一部分?

javascript - JQuery Combobox 触发另一个事件

javascript - 转义引号 - javascript、struts 2

javascript - IE 8 在 "slow"jQuery 脚本上提示用户

javascript - 无法从 SemanticUI 多下拉列表中获取值

javascript - 如何使用 CSS 动画淡出 div 幻灯片?

javascript - 组件有 Render 方法,但没有扩展 React.Component