在 Meteor 中启用跨源资源共享?
Enable cross-origin resource sharing in Meteor?
我正在尝试获取从我的主应用程序到外部 angular 应用程序的频道列表。
我已将 https://github.com/stubailo/meteor-rest/blob/master/packages/rest/README.md 添加到我的主要 meteor 应用程序,现在我可以使用 url 作为 json 格式获取集合。
现在,当我尝试从外部 angular 应用程序发出 http 请求时,问题就来了。
这是我的主要 meteor 应用程序中的内容:
'use strict'
Meteor.publish('channels', function (index) {
return Channels.find({});
}, {
url: 'channels',
httpMethod: 'get'
});
这是我在外部 angular 应用程序中发出 http 请求所使用的内容:
// Simple GET request example:
$http.get('http://example.com/channels').then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log('success');
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('error');
console.log(response);
});
但是我得到的响应是一个错误:
XMLHttpRequest cannot load http://example.com/channels. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我该怎么做才能解决这个问题?
来自 meteor-rest 包上的documentation:
If you would like to use your API from the client side of a different app, you need to return a special header. You can do this by hooking into a method on the simple:json-routes
package, like so:
// Enable cross origin requests for all endpoints
JsonRoutes.setResponseHeaders({
"Cache-Control": "no-store",
"Pragma": "no-cache",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, PUT, POST, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With"
});
我正在尝试获取从我的主应用程序到外部 angular 应用程序的频道列表。
我已将 https://github.com/stubailo/meteor-rest/blob/master/packages/rest/README.md 添加到我的主要 meteor 应用程序,现在我可以使用 url 作为 json 格式获取集合。
现在,当我尝试从外部 angular 应用程序发出 http 请求时,问题就来了。
这是我的主要 meteor 应用程序中的内容:
'use strict'
Meteor.publish('channels', function (index) {
return Channels.find({});
}, {
url: 'channels',
httpMethod: 'get'
});
这是我在外部 angular 应用程序中发出 http 请求所使用的内容:
// Simple GET request example:
$http.get('http://example.com/channels').then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log('success');
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('error');
console.log(response);
});
但是我得到的响应是一个错误:
XMLHttpRequest cannot load http://example.com/channels. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我该怎么做才能解决这个问题?
来自 meteor-rest 包上的documentation:
If you would like to use your API from the client side of a different app, you need to return a special header. You can do this by hooking into a method on the
simple:json-routes
package, like so:
// Enable cross origin requests for all endpoints
JsonRoutes.setResponseHeaders({
"Cache-Control": "no-store",
"Pragma": "no-cache",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, PUT, POST, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With"
});