2017-04-17 7 views
0

이것은 일반적인 질문입니다. 내 Swagger API는 IE11, Chrome, FireFox 외에도 IE10을 사용할 때 잘 작동하며 오류가 발생하면 failed to parse JSON/YAML이고 swagger.inspec().state은 다시 "rejected"이됩니다. 나는 또한 내가 API_URI 전체 URL을하지만, 단지 경로로 정의하지 않은 이유는 프록시 서버를 사용하고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; 
}; 

: 내 클라이언트를 인스턴스화하고 방법은 다음과

이다.

IE10이 지원 되더라도 IE10을 제외한 다른 모든 브라우저에서이 기능이 작동하는 이유는 무엇입니까?

+0

어떤 Swagger UI 버전입니까? 최신 3.0.x [IE10을 지원하지 않습니다] (https://github.com/swagger-api/swagger-ui#browser-support). – Helen

+0

아니요. "swagger-client"를 사용하고 있습니다. "^ 2.1.17"' https://github.com/swagger-api/swagger-js/issues/1018에서 조금 더 자세한 내용을 추가했습니다. –

답변

0

내 node_modules 폴더의 Swagger-JS 소스 코드를 통해 다이빙 한 후.

내가 발견 한 것은 초기화 방법이 생성자에 전달 된 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; 

I 오전의 gunna 시도하고이 고정 얻을 자신감하기 위해 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, 
});