nuxt.js localhost:3000からspringサーバlocalhost:8080のPOSTルーティングに対して、リクエストを送っているのですが、corsエラーで弾かれてしまいます。
PostMappingの部分をGetMappingにして、nuxt側のコードもgetでリクエストするようにすれば問題なくいきます。
そもそもコントローラクラスに@CrossOriginアノテーション追加しているので、どのHTTPメソッドでも行くはずと思っていました。
何かcsrf tokenのようなものの設定等必要なのでしょうか?
@CrossOrigin @RestController @RequestMapping("api") public class testController { @PostMapping("test") public String postTest(@RequestBody Object request) { System.out.print("aaa"); return "test"; } }
こちらnuxt側でリクエストしている箇所です。
await axios.post('http://localhost:8080/api/test', { body: this.body }).then((result) => { window.console.log(result.data) window.console.log('完了') }).catch((result) => { window.console.log(result.data) window.console.log('失敗') })
以下ブラウザで表示されているエラーです。
post:1 Access to XMLHttpRequest at 'http://localhost:8080/api/test' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。