使用CORS

时间:2017-01-13 15:32:41

标签: ajax xml internet-explorer cors tyk

我正在尝试向请求体中需要XML的API发送POST请求,并使用XML数据进行响应。 API位于另一个域上,因此必须使用CORS。 IE11发送成功的CORS预检。这是一个简化的测试用例,在Windows 7上的Internet Explorer 11上出现以下错误:

SCRIPT7002: XMLHttpRequest: Network Error 0x800c0007, No data is available for the requested resource.

    var body = '<?xml version="1.0" encoding="UTF-8"?> '
    + '<Trias version="1.1" xmlns="http://www.vdv.de/trias" xmlns:siri="http://www.siri.org.uk/siri" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> '
    + '    <ServiceRequest> '
    + '        <siri:RequestTimestamp>2016-06-27T13:34:00</siri:RequestTimestamp> '
    + '        <siri:RequestorRef>EPSa</siri:RequestorRef> '
    + '        <RequestPayload> '
    + '            <StopEventRequest> '
    + '                <Location> '
    + '                    <LocationRef> '
    + '                        <StopPointRef>8502113</StopPointRef> '
    + '                    </LocationRef> '
    + '                    <DepArrTime>2017-01-03T10:22:00</DepArrTime>'
    + '                </Location> '
    + '                <Params> '
    + '                    <NumberOfResults>1</NumberOfResults> '
    + '                    <StopEventType>departure</StopEventType> '
    + '                    <IncludePreviousCalls>true</IncludePreviousCalls> '
    + '                    <IncludeOnwardCalls>true</IncludeOnwardCalls> '
    + '                    <IncludeRealtimeData>true</IncludeRealtimeData> '
    + '                </Params> '
    + '            </StopEventRequest> '
    + '        </RequestPayload> '
    + '    </ServiceRequest> '
    + '</Trias> ';

    $(document).ready(function() {
      $('button').click(function() {
        $.ajax('https://odpch-api.begasoft.ch/trias-stackoverflow', {
          method: 'POST',
          headers: {
            'Content-Type': 'text/xml',
            'Authorization': '57c5dadd5e6307000100005e4365c41fa4d946a44ecbd19508fbef64'
          },
          data: body,
          dataType: 'text'
        }).done(function(data) {
          $('#response').text(data);
        });
      });
    });
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<button>Ajax</button>
<div id="response"></div>
</body>
</html>

编辑:从代码段运行它会产生另一个错误,这里是带有完整示例的pastebin:http://pastebin.com/yTL7mrYF

预检请求和发布请求本身在开发工具中可见。对于POST请求,只有响应头可见,但响应正文显示“无数据要查看”。

我在Fiddler中看到响应从服务器发回,但不知何故Internet Explorer无法显示它。

该服务支持Tyk,一种API管理软件。

显然这适用于Firefox和Chrome。

1 个答案:

答案 0 :(得分:2)

问题是压缩响应(deflate)。当我删除Accept-Encoding请求标头时,响应不会被压缩,并且可以在Internet Explorer中使用。