前提・実現したいこと
axiosを使ってMyjsonにポストしたい
発生している問題・エラーメッセージ
Failed to load resource: the server responded with a status of 400 (Bad Request)
Failed to load https://api.myjson.com/bins/1ayjia/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access. The response had HTTP status code 400.
該当のソースコード
react.js
1 getPost = title => { 2 let params = new URLSearchParams(); 3 const url = "https://api.myjson.com/bins/1ayjia/"; 4 params.append("title", title); 5 params.append("id", this.state.nextId + 1); 6 const data = {title: title,id: this.state.nextId + 1}; 7 axios({ 8 method :"POST", 9 url: url, 10 headers:{"Cache-Control":"max-age=0, private, must-revalidate","Content-Type":"application/json; charset=utf-8"}, 11 data : params, 12 responseType: 'json' 13 }).then(res=>{ 14 console.log(res.data.todos); 15 }).catch(error=>{ 16 console.log(error); 17 }); 18 this.setState({ nextId: this.state.nextId + 1 }); 19 };
試したこと
headers:をmyjsonapi
補足情報(FW/ツールのバージョンなど)
環境
・react.js
・axios
あなたの回答
tips
プレビュー