如何在Swagger UI 3.0版中为每个请求添加自定义标头

如何在Swagger UI 3.0版中为每个请求添加自定义标头,swagger,swagger-ui,Swagger,Swagger Ui,我想在我的swagger ui 3.0版上支持多个json版本。 第二个json在每次ajax调用时我都要添加一个自定义头 名称x-api-version和值2 我试过这个: $.ajaxSetup({ beforeSend: function(xhr) { xhr.setRequestHeader('x-my-custom-header', 'some value'); } }); 但它不起作用。 swagger ui 3.0.0的文档很差,经过一些实验后,我

我想在我的swagger ui 3.0版上支持多个json版本。 第二个json在每次ajax调用时我都要添加一个自定义头 名称
x-api-version
和值
2

我试过这个:

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('x-my-custom-header', 'some value');
    }
});
但它不起作用。
swagger ui 3.0.0的文档很差,经过一些实验后,我找不到解决方案,如果swagger使用jquery处理选项请求,而使用客户端HTTP请求库处理其他类型的请求,如GET、POST等

因此,要为Swagger UI中的每个请求添加自定义头,请使用以下代码:

    $(document).ready(function () {

    // Intercept jquery ajax request
    $.ajaxSetup({

        beforeSend: function (xhr, settings) {

            // Add the header to the Ajax request
            xhr.setRequestHeader("Authorization", getHeader());
        },

        // Disable caching of AJAX responses
        cache: false

    });

    // Intercept superagent request
    window.swaggerUi.options["requestInterceptor"] = function () {

        // Add the header to the request
        this.headers.Authorization = getHeader();

        return this;
    };

});

function getHeader() {

    var header = ... some code to calculate or generate a header ...

    return header
}

这在3.x中目前不受支持。请参阅上的讨论。可能重复的