私はdocker
コンテナで実行されているのシステムに、mysql
データベースを呼び出す8080
のポートを公開しました。春の起動時に「Access-Control-Allow-Origin」
私はlocalhost:8080/blogs
を打つとき、私はブラウザで直接それを打ったときにこれがうまく働いているバック[{"author":"Christopher Bolton","title":"Test Title 1","content":"This is some content","date":"2017-08-29"}]
を取得します。しかし、私がjQuery
から試してみると、私は普通のAccess-Control-Allow-Origin
を得ています。ここで
は私の春のブートサービスである:ここで
@SpringBootApplication
@RestController
public class ChrisboltonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ChrisboltonServiceApplication.class, args);
}
@Autowired
private JdbcTemplate jdbcTemplate;
@CrossOrigin
@RequestMapping(path="/blogs")
public @ResponseBody Iterable<ChrisBolton> getAllUsers() {
List<ChrisBolton> result = jdbcTemplate.query(
"SELECT * FROM blog",
(rs, rowNum) -> new ChrisBolton(rs.getString("author"),
rs.getString("title"),
rs.getString("content"),
rs.getDate("date"))
);
return result;
}
}
は私のjQuery
次のとおりです。
$.ajax({
url: "http://localhost:8080/blogs",
crossDomain: true
}).done(function(data) {
console.log(data);
});
しかし、私はまだ、このエラーを取得しています:
XMLHttpRequest cannot load http://localhost:8080/blogs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
私はでthisを試してみましたgetAllUsers()
メソッドに@CrossOrigin
を追加する私はクラスレベルで試しました。私はポート3000
で私のUIを実行しているので、thisも見ました。しかし、そのリンクは春に特有のものではありません。
EDIT
私のリクエストヘッダを追加する:ネットワークタブ(クローム)で
GET /blogs HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113
Safari/537.36
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
応答:私は、データを取得していますように
[{"author":"Christopher Bolton","title":"Test Title 1","content":"This is some content","date":"2017-08-29"}]
だから、見えます[ネットワーク]タブに戻ります。しかし、私のconsole.log(data)
アプリケーションにこれを追加することAccess-Control-Allow-Origin
'@ CrossOrigin'はまだありますか?まだ同じエラーが発生しています。 –
また、私はドッカーを使用してポート8080を公開しているという事実の上に追加しました。これは問題の一部になる可能性がありますか? –
実際、 '@ CrossOrigin'だけですべてが機能していたはずです。私はちょうどここでテストし、それは正常に動作します。ドッカーコンテナを使用するという事実は、それに影響を及ぼすはずです。コンテナベースのイメージは何ですか?コンテナの中の春のブーツの前にプロキシのようなものはありませんか? – acdcjunior