メインアプリケーションから外部アングルアプリへのチャンネルのリストを取得しようとしています。
https://github.com/stubailo/meteor-rest/blob/master/packages/rest/README.mdをメインのmeteorアプリに追加しました。これで、json形式のURLでコレクションを取得できます。
これで、外部の角度アプリからhttpリクエストを実行しようとすると問題が発生します。
これが私のメインの流星アプリにあるものです:
'use strict'
Meteor.publish('channels', function (index) {
return Channels.find({});
}, {
url: 'channels',
httpMethod: 'get'
});
これが、外部アングルアプリで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.
これを修正するにはどうすればよいですか?
流星レストパッケージのドキュメントから:
別のアプリのクライアント側からAPIを使用する場合は、特別なヘッダーを返す必要があります。次の
simple:json-routes
ように、パッケージのメソッドにフックすることでこれを行うことができます。
// 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"
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加