フロントがVue、 バックエンドがRailsで開発してます。
axiosを使い、Ajax通信で成功した場合のレスポンスを受け取りたいのですが、レスポンスのdata
が空になります。
##エラー内容
返ってくるレスポンス↓
](cb64b734bea8603b5845c0a67f441d1b.png)
##コード
app/controllers/api/ideas_controller.rb
1axios.post(`api/ideas`, params) 2 .then(res => { 3 console.log(res.data) 4 // => "" 5 }) 6 .catch(error => { 7 console.log(error.response.data) 8 // => ["xxx"が入力されてません"] 9 }
controller
1def create 2 @idea = current_user.ideas.new(idea_params) 3 if @idea.save 4 render formats: :json, handlers: :jbuilder 5 head :created 6 else 7 render json: @idea.errors.full_messages, status: :bad_request 8 end 9end 10
app/views/api/ideas/create.json.jbuilder
1json.title @idea.title 2json.description @idea.description 3json.userImage current_user.social_profiles.first&.image_url 4json.userName current_user.name 5json.likeCount 0 6json.isLiked false 7json.comments 0
エラーはしっかり返ってきます。(error.response.data
)
##試したこと
###ヘッダーにCSRFトークンを含める
axios.defaults.headers['X-CSRF-TOKEN'] = $('meta[name=csrf-token]').
変化なし。
また、curlでPOSTしてみるのですが、レスポンスが何も返ってきません。。
curl -X POST -H "Content-Type: application/json" -d '{"idea": {"title":"test", "description":"100"} }' localhost:3000/api/ideas
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/17 05:38