angularjs - 2 个端口上的 Node Rest api 和 angularJS 应用程序的 CORS 问题

标签 angularjs node.js api rest cors

我的 angularJS 应用程序(在 localhost:9000 上)遇到了经典的 CORS 问题,试图捕获我的 Node 服务器(在 localhost:9001 上)

这是我的其余 api (app.js) 代码:

var cors = require('cors');
app.use(cors());
app.options('*', cors());

app.all('/*', function(req, res, next) {
    // CORS headers
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header('Access-Control-Allow-Methods',    'GET,PUT,POST,DELETE,OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
    if (req.method == 'OPTIONS') {
        res.status(200);
        res.write("Allow: GET,PUT,POST,DELETE,OPTIONS");
        res.end();
    } else {
        next();
    }
});

如您所见,我尝试了这些解决方案但没有成功:

这是 webapp 中简单的 $http 调用:

var req = {
    method: 'GET',
        url: 'localhost:9001/memories'
    };
    $http(req).
        success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
            reutrnStatus.success = true;
            reutrnStatus.msg = status;
            memories = data;
        }).
        error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            reutrnStatus.success = false;
            reutrnStatus.msg = status;
        });

我也在 webapp 中尝试了一些解决方案(在 app.js 中):

// Enable AngularJS to send its requests with the appropriate CORS headers
// globally for the whole app:
.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    /**
     * Just setting useXDomain to true is not enough. AJAX request are also
     * send with the X-Requested-With header, which indicate them as being
     * AJAX. Removing the header is necessary, so the server is not
     * rejecting the incoming request.
     **/
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

但我快要放弃了,因为它仍然不起作用......这给了我同样的错误:

XMLHttpRequest cannot load localhost:9001/memories. Cross origin
requests are only supported for protocol schemes: http, data, chrome, 
chrome-extension, https, chrome-extension-resource.

为了完全清楚,我遵循了其余 api 的教程:

最佳答案

您正在向未知协议(protocol)发出请求。

仅协议(protocol)方案支持跨源请求:http、data、chrome、chrome-extension、https、chrome-extension-resource。

您需要更改此设置:

method: 'GET',
    url: 'localhost:9001/memories'
};

...对此:

method: 'GET',
    url: 'http://localhost:9001/memories'
};

或者,您可以将其设置为 //localhost:9001/memories ,浏览器将使用当前协议(protocol),如果您通过 http 提供资源,则该协议(protocol)非常有用> 和 https

虽然不相关,但您的回调在变量名称中存在拼写错误。

关于angularjs - 2 个端口上的 Node Rest api 和 angularJS 应用程序的 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953669/

相关文章:

node.js - 客户端 session NodeJS/Express - 错误 : Cannot set property 'mydata' of undefined

javascript - 如何在模拟 Angular 服务中从 JSON fixture 中提取数据

css - Angular-Material Layout Behavior....兄弟行为问题

javascript - node.js - 按时附加事件处理程序

node.js - 太多回调..nodeJS airtable api

Python - 简单的 for 循环未按预期工作(使用请求模块)

php - laravel VerifyCsrfToken 使用通配符排除 url?

ruby-on-rails - ENV 变量中的 Capistrano 和 API key ?

javascript - 如何将 $resource 转换为 $http?

java - 如何在url参数中发送阿拉伯消息