2017-04-17 184 views
0

这是更常见的问题。当我使用IE11,Chrome,FireFox和IE10时,我的Swagger API工作正常,但我收到错误:failed to parse JSON/YAML,而我的swagger.inspec().state正在返回"rejected"IE10 Swagger错误无法解析JSON/YAML

这里是我如何实例化我的客户:

import { API_URI } from '../config/app_config'; // '/accountservice/swagger.json' 
import Swagger from 'swagger-client'; // "swagger-client": "^2.1.17" 

export const buildAccountServiceClient =() => { 
    const swagger = new Swagger({ 
    url: (!window.location.origin ? IE_API_URI : API_URI), 
    usePromise: true, 
    }); 

    // Reconfigure swagger client to override service path if we're using a reverse proxy: 
    // /accountservice/swagger.json -> /accountservice 
    // Originally tried setting basePath to null, undefined, and '', but that didn't work 
    let basePath; 
    if (API_URI.startsWith('/')) { 
    basePath = API_URI.substring(0, API_URI.lastIndexOf('/')); 
    swagger.then((client) => { 
     client.setBasePath(basePath); 
     if (typeof(window) !== 'undefined') { 
     // use current protocol, so either http or https 
     client.setSchemes([window.location.protocol.slice(0, -1)]); 
     } 
    }); 
    } 
    return swagger; 
}; 

我也使用这就是为什么我有API_URI没有定义为完整的URL,但只是路径中的代理服务器。

为什么在IE10以外的所有其他浏览器中都能正常工作?

+0

哪个版本的Swagger UI?最新的3.0.x [不支持IE10](https://github.com/swagger-api/swagger-ui#browser-support)。 – Helen

+0

nope我正在使用'“swagger-client”:“^ 2.1.17”' 我在这里添加了更多的上下文:https://github.com/swagger-api/swagger-js/issues/1018 –

回答

0

通过Swagger-JS源代码在我的node_modules文件夹中潜水后。

我发现初始化方法是试图抓住当前窗口位置“原点”以添加到传递给构造函数的url路径的前面。

由于IE10窗口对象中没有origin键,所以它将URL转换为undefined/swagger.json

为了解决这个问题我做了以下变化:

node_modules/swagger-client/lib/client.js line:129

line 126 if(this.url && this.url.indexOf('http:') === -1 && this.url.indexOf('https:') === -1) { 
    line 127 // no protocol, so we can only use window if it exists 
    line 128 if(typeof(window) !== 'undefined' && window && window.location) { 
    line 129  this.url = window.location.protocol + "//" + window.location.host + this.url; 
    line 130 } 
    line 131 } 

在IE10中,窗口对象不包含名为location.origin当其他浏览器做一个对象键。

所以,如果你想获得这在IE10工作:

line 129 this.url = window.location.protocol + '//' + window.location.host + this.url; 

我贡纳尝试,并提交一个PR招摇修复此问题。

--UPDATE--

如果你不想等待PR经历,需要在您的最终修复:

http://tosbourn.com/a-fix-for-window-location-origin-in-internet-explorer/

或者这里是我的修补程序:

const API_URI = '/swagger.json'; 
const IE_API_URI = `${window.location.protocol}//${window.location.hostname}${(window.location.port ? `:${window.location.port}` : '')}/swagger.json`; 

const swagger = new Swagger({ 
    url: (!window.location.origin ? IE_API_URI : API_URI), 
    usePromise: true, 
});