【问题标题】:Creating a SOAP XMLHttpRequest request in JavaScript在 JavaScript 中创建 SOAP XMLHttpRequest 请求
【发布时间】:2018-06-18 20:37:58
【问题描述】:

我正在尝试在 JavaScript 中创建一个 SOAP 请求,但我得到了错误的响应。

这是我的要求:

callSOAP() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', 'https://webapi.allegro.pl/service.php', true);
    var sr = 
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<SOAP-ENV:Envelope ' + 
        'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' + 
        'xmlns:main="https://webapi.allegro.pl/service.php" ' +
        'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
        'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
        '<SOAP-ENV:Body>' +
            '<main:DoGetCountriesRequest>' +
                '<main:countryCode>1</main:countryCode>' +
                '<main:webapiKey>xxxxxxxx</main:webapiKey>' +
            '</main:DoGetCountriesRequest>' +
        '</SOAP-ENV:Body>' +
    '</SOAP-ENV:Envelope>';

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.response);
        }
    };
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(sr);
}

我尝试调用“DoGetCountriesRequest”方法,但响应是状态代码 500,并显示消息“无效 XML”。

在 JavaScript 中调用 SOAP 方法是否正确?我的请求有什么问题?

【问题讨论】:

    标签: javascript soap xmlhttprequest


    【解决方案1】:

    看起来您正在将请求发送到 ?wsdl 端点 - 从您的 xmlhttp.open() 方法调用中的 URL 中删除它以将其发送到服务本身。

    您的 SOAP 消息似乎格式不正确 - 您过早关闭了 SOAP-ENV:Envelope 开始标签 - 它还需要围绕您的 xmlns:xsixmlns:xsd 命名空间定义:

    '<?xml version="1.0" encoding="utf-8"?>' +
        '<SOAP-ENV:Envelope ' + 
            'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:main="https://webapi.allegro.pl/service.php"' +
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' + ...
    

    后续修改: 您的 countryCode 和 webapiKey 开始标记中有一些双引号需要删除,并且看起来消息本身不符合 WSDL - WSDL 中的操作 doGetCountries 需要一个 DoGetCountriesRequest 对象。尝试类似:

    var sr = 
        '<?xml version="1.0" encoding="utf-8"?>' +
        '<SOAP-ENV:Envelope ' + 
            'xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' + 
            'xmlns:main="https://webapi.allegro.pl/service.php" ' +
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
            '<SOAP-ENV:Body>' +
                '<main:DoGetCountriesRequest>' +
                    '<main:countryCode>1</main:countryCode>' +
                    '<main:webapiKey>xxxxxxxx</main:webapiKey>' +
                '</main:DoGetCountriesRequest>' +
            '</SOAP-ENV:Body>' +
        '</SOAP-ENV:Envelope>';
    

    【讨论】:

    • 我修复了这个问题,现在我收到一个错误,状态码为 500,消息为“无效 XML”。有什么想法可能还缺少吗?
    • 根据您的评论更新了我的答案。
    • 相同的 500 响应。 :|
    • 如果请求现在符合要求,那么我怀疑这可能与 CORS 有关 - 看起来 SOAP 服务正在返回 500 和 OPTIONS 请求,我在 Chrome 中的测试将其解释为错误对请求进行飞行前检查,然后不使用其配置的 CORS 数据发出第二个 POST 请求。
    猜你喜欢
    • 1970-01-01
    • 2014-05-22
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多