実現したいこと
axiosでSpringBootのサーバへPOSTし、エラー応答が返却されたときにcatchに入るようにしたい。
発生している問題・分からないこと
axiosでformの情報をSpringBootのサーバへPOSTし、エラー応答が返却されてもcatchに入らない。
200応答が返却されるときはコード内のresponseに値が格納されるが、エラー応答の時はundefinedになってしまう。
SpringBoot側でPOSTしたデータは確認できておりPOSTはできているはず。
該当のソースコード
jsx
1const fun = async (e) => {2 e.preventDefalt();3 const form = e.target;4 const formData = new FormData(form);5 let response;6 try {7 response = await axios.post("/post", {8 data1: "aaa",9 data2: "bbb"10 });11 }12 catch (error) {13 console.log("aaa"); <-ここが出力されない 14 }15}16return(17 <div>18 <form onSubmit={fun}> 19 <button type="submit"></button>20 </form>21 </div>22);23
java
1@RestController2public class A {3 @PostMapping("/post")4 public ResponseEntity<String> a(@RequestBody Data data) {5 return new ResponseEntity("aaa", HttpStatusCode.BAD_REQUEST);6 // return new ResponseEntity("aaa", HttpStatusCode.OK);7 }8}
試したこと・調べたこと
上記の詳細・結果
async/awaitの書き方でなくaxios.post().then().catchの書き方も試した。
補足
特になし

0 コメント