2017-08-09 12 views
0

私は、何らかの理由でリクエストが失敗したとしても、大量のjsonデータを返すコアリクエストを作成しようとしています。CORS GET request in jQuery

catcher.js:197 OPTIONS http://test/v/calls 401 (Unauthorized) 

そして:

XMLHttpRequest cannot load http://test/v/calls. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://some_domain' is therefore not allowed access. The response had HTTP status code 401. 

注:認証パラメータと同じURLを以下のように

$(document).ready(function() { 
var url = 'http://test/v/calls'; 
$.ajax({ 
    url: url, 
    beforeSend: function (request) { 
     request.setRequestHeader("Authorization", "Bearer test"); 
    }, 
    success: function (json) { 
     console.log("Success", json); 
    }, 
    error: function (textStatus, errorThrown) { 
     console.log(textStatus, errorThrown); 
    } 
}); 

応答は次のように私がこれまで行ってきたコードがあります郵便配達員でうまく動作します。何が間違っているのか分かりません。

答えて

0

サーバーでcorsを有効にする必要があります。これはPostmanのプロトコルがクロスサイトスクリプティングを処理する方法のためにPostmanで機能します。あなたのサーバーは何を構築していますか?エンドポイントにコルスを追加するにはどうすればいいですか?あなたが始めることができる2つの質問です。

+0

私は、この投稿に従って、vhost.confの '' Header set Access-Control-Allow-Origin "*" 'を追加しました:[link](https://enable-cors.org/server_apache.html)。その後、私はコマンド 'ln -sf /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load'を実行しました。私が得る応答は同じです。 –

+0

私はちょっとデバッグしました。私はこれを見つけました: 'Www-Authenticate:Bearer realm =" api "'。それはどこから来たのですか?それはテストではありませんか? –