Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用Swagger js启用浏览器cookie_Angularjs_Http_Cookies_Swagger - Fatal编程技术网

Angularjs 使用Swagger js启用浏览器cookie

Angularjs 使用Swagger js启用浏览器cookie,angularjs,http,cookies,swagger,Angularjs,Http,Cookies,Swagger,我在AngularJs SPA中使用swagger js/swagger client()使用TryItOut执行器向nodeJS服务器发送REST请求 我正在使用PassportJS进行身份验证。当我使用Angular的$http服务并将withCredentials属性设置为true时: $http({ method: 'GET', withCredentials: true, url: 'http://someUrl/someRestPath

我在AngularJs SPA中使用swagger js/swagger client()使用TryItOut执行器向nodeJS服务器发送REST请求

我正在使用PassportJS进行身份验证。当我使用Angular的$http服务并将withCredentials属性设置为true时:

$http({
        method: 'GET',
        withCredentials: true,
        url: 'http://someUrl/someRestPath/' })
,一切正常-浏览器cookie已正确设置,并且每个进一步的请求都已成功验证

切换到swagger js Executor,我无法正确获得身份验证,因为没有设置cookies。我尝试了以下方法:

var swaggerClient = new SwaggerClient(specUrl)
  .then(function (swaggerClient) {                                            
      swaggerClient.http.withCredentials = true; // this activates CORS, if necessary
如文件中所述,但这不起作用。虽然我在使用passport进行身份验证时收到凭据,但由于未设置cookie,进一步的请求未经授权


如何使用swagger js启用此行为?

我找到了一个解决方案。Swagger js客户端的execute方法允许我们定义自己的http方法。我只是在.config中全局使用angular的$http服务,并将withCredentials设置为true

import Swagger from 'swagger-client';
在.config中:

angular.module('app', []).config(['$httpProvider',function ($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
}
然后,当您创建您的招摇过市客户机时:

Swagger({ url: 'some swagger specification (yaml/json)' }).then(client => {
    client.execute({ http: $http, operationId: operationId, parameters: parameters});