ノードWebサーバー(Express)がポート6000
で実行されています。ローカルホスト上の別のポートに要求を送信するときにCORSヘッダー 'Access-Control-Allow-Origin'が表示されない
var xhr = new XMLHttpRequest();
var url = 'http://localhost:4000/endpoint';
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log('endpoint');
callback(xhr.responseText);
}
};
xhr.send(JSON.stringify({'key': 'value'}));
がポート4000
上で実行している別の急行のサーバーがあります:http://localhost:6000/
が呼び出されると
は、index.html
は実行するスクリプト、logic.js
を持っているクライアントに提供しています。
サーバは単に要求本体が送信した内容を返すエンドポイント設定します:私はhttp://localhost:6000/
にアクセスすると
app.route('/segment')
.post(bodyParser.json(), function(req, res) {
res.set('Access-Control-Allow-Origin', '*');
res.send(req.body);
});
を、私は、ブラウザのコンソールでこれを見ている:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:4100/segment. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
がありますプロトタイプ作成中にこれを回避する方法は?