クロス ドメイン リクエストを作成しようとしています。私は CORS を認識しており、たとえば w3schools からの呼び出しをドメインとしてテストしたいと考えています。
REST API と Apache サーバーに Jersey を使用しており、次のことを行いました。
ResponseBuilder rb = Response.status(status).entity(mapper.writeValueAsBytes(data)).type(MediaType.APPLICATION_JSON);
**EDITED **
rb.header("Access-Control-Allow-Origin","*");
rb.header("Access-Control-Allow-Methods","GET, OPTIONS");
rb.header("Access-Control-Allow-Headers","*");
return rb.build
ブラウザーから API を呼び出すだけで応答ヘッダーを表示すると、
Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:GET, OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:2523
Content-Type:application/json
Date:Wed, 2013 年 4 月 17 日 20:16:20 GMT
有効期限:1969 年 12 月 31 日水曜日 16:00:00 PST
サーバー:Apache-Coyote/1.1
しかし、別のドメイン、つまりW3スクールを使用して実際にテストしたい場合、次のものがあります-
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "url", // this is a real URL replaced with just url
type: 'GET',
success: function(content) { console.log("ok"); },
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("error");
},
complete: function() { console.log("complete"); }
});
});
});
</script>
</head>
<body>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
私はいつも XMLHttpRequest cannot load :url: を取得します。Origin http://www.w3schools.comは Access-Control-Allow-Origin で許可されていません。
私のリクエストヘッダーは
Accept: */*
Origin: http://www.w3schools.com
Referer: http://www.w3schools.com/jquery/tryit_view.asp?x=0.8691769954748452
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64 ) AppleWebKit/537.31 (Gecko のような KHTML) Chrome/26.0.1410.64 Safari/537.31
上記をjsfiddleでもテストしようとしましたが、同じエラーが発生しました。
注: 私の URL はまだローカルホストにあるだけですが、それが問題である可能性はありますか?
私は何が欠けていますか?