Tag: cors

POST请求使用Fetch API无法正常工作

我有一些重大的问题,理解为什么Fetch API不会让我发送POST请求到我的restify服务器。 我有一个基本的restify服务器,在/login上接收POST请求的路由。 如果我使用Postman或HTTPRequester进行testing,但是当我使用获取API在浏览器应用程序上对其进行testing时,此路由完全按照预期运行,但出现以下错误(在Chrome中): OPTIONS http://localhost:1337/login 405 (Method Not Allowed) Fetch API cannot load http://localhost:1337/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set […]

的NodeJS。 错误:无法validation第一个证书

我正在用AngularJS编写一个Web应用程序,而且由于CORS问题,我使用了NodeJS。 那么,当我试图在我的节点发出请求,我得到这个错误: events.js:160 throw er; // Unhandled 'error' event ^ Error: unable to verify the first certificate at Error (native) at TLSSocket.<anonymous> (_tls_wrap.js:1092:38) at emitNone (events.js:86:13) at TLSSocket.emit (events.js:185:7) at TLSSocket._finishInit (_tls_wrap.js:610:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) 我的NodeJS文件(index.js): var express = require('express') var cors = require('cors') var bodyparser = require('body-parser'); var querystring = require('querystring'); var rootCas = […]

与同一服务器上运行的socket.io(节点)和apache发生CORS错误

我正在为我的网站创build一个function,让人们可以与我聊天。 为了做到这一点,我用php和apachebuild立了网站的常规部分,并且用node.js(使用socket.io和express)build立了聊天服务器。 当人们去我们的网站,有一个小酒吧,如果他们想和我聊天(在同一个窗口,而不是popup窗口)从侧面滑出。 聊天服务器正在运行,我看到客户端正在尝试连接: info – socket.io started Express server listening on port 8000 in development mode debug – client authorized info – handshake authorized 776963641537662118 它不会再进一步​​,尝试通过websockets或XHR连接,并在控制台的客户端上出现以下错误: XMLHttpRequest cannot load http://wundertutor.com:8000/socket.io/1/?t=1356208544112. Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true. 另外,我在我的.htaccess文件中有这个: <IfModule mod_headers.c> Header set Access-Control-Allow-Origin "*" </IfModule>

在ExpressJS中接受CORS请求的自定义HttpHeaders

我有一个ExpressJS应用程序通过REST暴露一些数据。 当我从同一个来源访问REST接口或使用curl时,一切正常,并且我的自定义http头被发送到我的路由。 但是,当我尝试从另一个来源访问REST接口时,我的标题被删除。 expressjsconfiguration看起来像这样 app.use(express.bodyParser()); var allowCrossDomain = function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET'); res.header('Access-Control-Allow-Headers', 'Accept'); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } }; app.all('/',allowCrossDomain); app.get('/foo', function(req,res){ console.dir(req.headers); }); 我尝试访问使用下面的JS代码的API var properties = { url: 'http://localhost:3001/foo?callback=?, type: 'GET', dataType: 'jsonp', headers: { "x-foo" : "FooBar"} }; $.ajax(properties).done(onSuccess).fail(function(r, s, e) […]

限制CORS不适用于POST请求

我正在使用restify为我的应用程序。 交叉原点请求对于使用restify的CORS的GET工作正常,但对POST请求显示以下错误。 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:1337/path/add. This can be fixed by moving the resource to the same domain or enabling CORS. 我使用的启用CORS的代码是: server.use(restify.CORS()); 根据其文件 。 任何人都可以build议我如何使POST请求工作

预检响应中的Access-Control-Allow-Origin不支持跨域访问

我正在尝试使用AJAX发送CORS请求到nodeJS服务器。 我想返回一些JSON数据。 我在网上发现了大量的教程,都说同样的事情,我已经尝试过,但我不能得到这个工作。 这是AJAX请求: $.ajax({ url: "http://some.other.url.com:8880", type: "GET", crossDomain: true, contentType: 'application/json' }).then(function(response) { $scope.allData = jQuery.parseJSON( response ); console.log($scope.allData); }).fail(function(response) { }); 这里是服务器上的代码: var path = url.parse(req.url).pathname, match = router.match(path), rescode; console.log("—: " + req.method); if (req.method === 'OPTIONS') { var headers = {}; headers["Access-Control-Allow-Origin"] = "*"; headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, […]

XMLHttpRequest无法加载http://127.0.0.1:1337/。 请求的资源上没有“Access-Control-Allow-Origin”标题

我尝试使用Angular.js和本地运行的Node.js服务器从HTML页面发送和接收数据 app.controller("MainController", function MainController($scope, $http){ $scope.postIt = function() { //alert("posting…"); $http.post('http://127.0.0.1:1337/', {msg:'hello from Angular.js!'}) .success(function onSuccess(response){ console.log(response); $scope.$apply(function(){ $scope.testData = JSON.parse(response); console.log($scope.testData); }); }).error(function onError(){ console.log("AJAX failed!"); }); }; }); <div id='content' ng-app='MyTutorialApp' ng-controller='MainController'> <p> <a ng-click="postIt()"> Click here to load data. </a> </p> </div> <script src="bower_components/angular/angular.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <script src="maincontroller.js" type="text/javascript"></script> 那么我得到错误: […]

NodeJS / Express PUT请求失败:请求的资源上没有“Access-Control-Allow-Origin”标头

我正在尝试使用NodeJS / Express构build一个服务(驻留在不同于调用该站点的子域上)。 我的GET和POST方法工作正常,但是我对PUT有点麻烦。 我试图实施corser,但我仍然收到以下错误: XMLHttpRequest无法加载http://services.example.org/jobs/_id/5503bb957e4eacd821b5c046 。 请求的资源上没有“Access-Control-Allow-Origin”标题。 Origin'http: //example.org '因此不被允许访问。 以下是我的corser实现。 我明白,其中一些可能是多余的或不必要的。 我只是想find魔术设置,使PUT请求成功完成。 app.use(corser.create({ simpleRequestHeaders: corser.simpleRequestHeaders.concat(["PUT"]), simpleRequestHeaders: corser.simpleRequestHeaders.concat(["OPTIONS"]), simpleResponseHeaders: corser.simpleResponseHeaders.concat(["PUT"]), simpleResponseHeaders: corser.simpleResponseHeaders.concat(["OPTIONS"]), requestHeaders: corser.simpleRequestHeaders.concat(["X-Requested-With"]) })); app.all('*', function(request, response, next) { response.header('Access-Control-Allow-Origin', '*'); response.header('Access-Control-Allow-Headers', 'X-Requested-With'); response.header('Access-Control-Allow-Headers', 'Content-Type'); response.header('Access-Control-Allow-Methods', 'GET'); response.header('Access-Control-Allow-Methods', 'POST'); response.header('Access-Control-Allow-Methods', 'PUT'); response.header('Access-Control-Allow-Methods', 'DELETE'); response.header('Access-Control-Allow-Methods', 'OPTIONS'); next(); });

当使用angular度$ http请求时,护照req.isAuthenticated始终返回false

我正在使用passportjs进行用户身份validation。 通过使用邮递员 ,我可以成功login和req.isAuthenticated总是返回我在login后的子序列请求为true。 通过使用angular$ http ,但是,login工作正常,但req.isAuthenticated总是在子序列请求中返回false。 我的angular度应用程序(localhost:3000)和节点应用程序(heroku)在不同的域。 我的想法是,它可能涉及CORS或会话cookie,因为我没有看到我的浏览器中的任何cookie。 我做了什么 我试图设置请求头允许CORS。 我也尝试在$ http和node app两个angular度设置Access-Control-Allow-Credentials 。 不幸的是,所有的尝试失败T_T Nodejs设置 app.use(function(req, res, next) { res.header('Access-Control-Allow-Credentials', true); res.header('Access-Control-Allow-Origin', req.headers.origin); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } }); passport.serializeUser(function(account, done) { done(null, account.id); }); // used to deserialize the user […]

AngularJS html5mode重新加载页面

我有两个单独的应用程序: AngularJS客户端工作在http:// localhost:8080 工作在http:// localhost:3000上的NodeJS API服务器 NodeJS API服务器只是给客户端提供json数据(并不提供任何html页面)。 因此,为了让客户端能够与服务器通信,我在API服务器上启用了CORS技术。 app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); next(); }); 另外我想使用干净的URL没有#符号,所以我启用我的AngularJS应用程序中的html5mode function config($stateProvider, $urlRouterProvider, $locationProvider) { $locationProvider.html5Mode(true); $urlRouterProvider.otherwise('/login'); … — index.html的 <!doctype html> <html lang="en" ng-app="app"> <head> <base href="/"> <title>APP</title> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/fontello.css"> </head> <body> […]