无法获取SOAP请求的响应文本

Unable to get response text of SOAP request

本文关键字:响应 文本 请求 SOAP 获取      更新时间:2023-09-26

这是不返回任何内容的代码。我在SOAP UI中使用了相同的SOAP请求,并且得到了正确的响应只是它没有出现在javascript中。

    var getmarket = new XMLHttpRequest();
    getmarket.open('POST', 'https://www.betfair.com/publicapi/', true);
    var m_request = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
                    'xmlns:bfex="http://www.betfair.com/publicapi/v5/BFExchangeService/" '+
                    'xmlns:v5="http://www.betfair.com/publicapi/types/exchange/v5/">'+
                    ' <soapenv:Header/>'+
                    '<soapenv:Body>'+
                    '<bfex:getAllMarkets>'+
                    '<bfex:request>'+
                    '<header>'+
                       '<clientStamp>0</clientStamp>'+
                       '<sessionToken>Y9eTuEvlrTM55pbRB1kIj0As0bVvz3eFm+p1FY+svHk=</sessionToken>'+
                    '</header>'+
                    '<locale>en</locale>'+
                    '<eventTypeIds>'+
                       '<v5:int>1</v5:int>'+
                    '</eventTypeIds>'+
                    '<countries>'+
                       '<v5:Country>GBR</v5:Country>'+
                    '</countries>'+
                    '<fromDate>2012-08-23TO00:00:00.000Z</fromDate>'+
                    '<toDate>2012-08-24TO00:00:00.000Z</toDate>'+
                 '</bfex:request>'+
              '</bfex:getAllMarkets>'+
           '</soapenv:Body>'+
        '</soapenv:Envelope>';
    getmarket.setRequestHeader('Content-Type', 'text/xml');
    getmarket.send(m_request);
    document.write(getmarket.responseText);

此外,当我使用document.write(m_request)//肥皂信封

我得到

0Y9eTuEvlrTM55pbRB1kIj0As0bVvz3eFm+p1FY+svHk=en1GBR2012-08-23TO00:00:00.000Z2012-08-24TO00:00:0.000Z

即所需字段之间的数据集

那么这可以吗?还是必须有更好的方法?

正如我在评论中提到的,Ajax是异步的,因此在js中,您必须执行以下操作:

getmarket.onreadystatechange = function (){
    if (getmarket.readyState == 4 && getmarket.status == 200)
         document.write(getmarket.responseText);
}

每次readyState更改时,onreadystatechange事件都会触发。