質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Vue CLI

Vue CLIは、Vue.jsでアプリケーション開発を行うためのコマンドラインインタフェース(CLI)に基づいた開発ツールです。インタラクティブなプロジェクトの雛形や設定なしで使用できるプロトタイプの作成など、さまざまな機能が用意されています。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

1360閲覧

Vue.js(cli)とlaravelにて簡単なtodoアプリを開発中ですが405番のエラーが出てtodoが追加できません。

abrt29

総合スコア12

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Vue CLI

Vue CLIは、Vue.jsでアプリケーション開発を行うためのコマンドラインインタフェース(CLI)に基づいた開発ツールです。インタラクティブなプロジェクトの雛形や設定なしで使用できるプロトタイプの作成など、さまざまな機能が用意されています。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/04/06 21:36

編集2021/04/07 13:06

vue.cliとLaravelで「追加・更新・削除」の機能のついたtodoアプリを開発中なのですが実際に追加ボタンをクリックした際下記のようなエラーが出ます。
chromeの検証ツール内のconsole内をコピーしたものになります。
ローカルサーバーでvueとlaravel同時に立ち上げ検証しております。
お力添え頂けると助かります。

xhr.js?b50d:177 POST http://127.0.0.1:8000/api/todo/[object%20Object] 405 (Method Not Allowed)

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v- on handler (Promise/async): "Error: Request failed with status code 405"
found in
---> <App> at src/App.vue
<Root>

vue.runtime.esm.js?2b0e:1888 Error: Request failed with status code 405
at createError (createError.js?2d83:16)
at settle (settle.js?467f:17)
at XMLHttpRequest.handleLoad (xhr.js?b50d:62)

以下、vue.jsのApp.vue内です

Vue

1<template> 2 <div id="app"> 3 <div id="bg"> 4 <div class="todo"> 5 <p>Todo List</p> 6 <div class="flex"> 7 <input type="text" name="insert" v-model="newTodo"> 8 <button class="btn insert" @click="insertTodo">追加</button> 9 </div> 10 <div class="list" v-for="item in todoLists" :key="item.id"> 11 <div class="flex"> 12 <input type="text" v-model="item.todo" > 13 <div class="container"> 14 <button class="btn update" @click="updateTodo(item.id, item.todo)">更新</button> 15 <button class="btn delete" @click="deleteTodo(item.id)">削除</button> 16 </div> 17 </div> 18 </div> 19 </div> 20 </div> 21 </div> 22</template> 23 24<script> 25import axios from "axios"; 26export default { 27 data() { 28 return { 29 newTodo: "", 30 todoLists: [], 31 }; 32 }, 33 methods: { 34 async getTodo() { 35 const resData = await axios.get("http://127.0.0.1:8000/api/todo/"); 36 this.todoLists = resData.data.data; 37 }, 38 async insertTodo() { 39 const sendData = { 40 todo: this.newTodo, 41 }; 42 await axios.post("http://127.0.0.1:8000/api/todo/" + sendData); 43 await this.getTodo(); 44 }, 45 async updateTodo(id, todo) { 46 const sendData = { 47 todo: todo, 48 }; 49 await axios.put("http://127.0.0.1:8000/api/todo/" + id, sendData); 50 await this.getTodo(); 51 }, 52 async deleteTodo(id) { 53 await axios.delete("http://127.0.0.1:8000/api/todo/" + id); 54 await this.getTodo(); 55 }, 56 }, 57 created() { 58 this.getTodo(); 59 }, 60}; 61</script>

以下、Laravel内の記述です
マイグレーション

Laravel

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateTodosTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('todos', function (Blueprint $table) { 17 $table->id(); 18 $table->string('todo'); 19 $table->timestamps(); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('todos'); 31 } 32}

cors

laravel

1<?php 2return [ 3 'paths' => ['*'], 4 'allowed_methods' => ['*'], 5 'allowed_origins' => ['*'], 6 'allowed_origins_patterns' => [], 7 'allowed_headers' => ['*'], 8 'exposed_headers' => false, 9 'max_age' => false, 10 'supports_credentials' => false, 11];

コントローラー

laravel

1<?php 2 3namespace App\Http\Controllers; 4 5use App\Models\Todo; 6use Illuminate\Http\Request; 7 8class TodoController extends Controller 9{ 10 /** 11 * Display a listing of the resource. 12 * 13 * @return \Illuminate\Http\Response 14 */ 15 public function index() 16 { 17 $items = Todo::all(); 18 return response()->json([ 19 'message' => 'OK', 20 'data' => $items 21 ], 200); 22 } 23 24 /** 25 * Store a newly created resource in storage. 26 * 27 * @param \Illuminate\Http\Request $request 28 * @return \Illuminate\Http\Response 29 */ 30 public function store(Request $request) 31 { 32 $item = new Todo; 33 $item->todo = $request->todo; 34 $item->save(); 35 return response()->json([ 36 'message' => 'Created succesfully', 37 'data' => $item 38 ], 200); 39 } 40 41 /** 42 * Display the specified resource. 43 * 44 * @param \App\Models\Todo $todo 45 * @return \Illuminate\Http\Response 46 */ 47 public function show(Todo $todo) 48 { 49 // 50 } 51 52 /** 53 * Update the specified resource in storage. 54 * 55 * @param \Illuminate\Http\Request $request 56 * @param \App\Models\Todo $todo 57 * @return \Illuminate\Http\Response 58 */ 59 public function update(Request $request, Todo $todo) 60 { 61 $item = Todo::where('id', $todo->id)->first(); 62 $item->todo = $request->todo; 63 $item->save(); 64 if ($item) { 65 return response()->json([ 66 'message' => $item, 67 ], 200); 68 } else { 69 return response()->json([ 70 'message' => 'Not found', 71 ], 404); 72 } 73 } 74 75 /** 76 * Remove the specified resource from storage. 77 * 78 * @param \App\Models\Todo $todo 79 * @return \Illuminate\Http\Response 80 */ 81 public function destroy(Todo $todo) 82 { 83 $item = Todo::where('id', $todo->id)->delete(); 84 if ($item) { 85 return response()->json([ 86 'message' => $item, 87 ], 200); 88 } else { 89 return response()->json([ 90 'message' => $item, 91 ], 404); 92 } 93 } 94}

api.php

laravel

1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\TodoController; 5 6Route::apiResource('/todo', TodoController::class);

error

1POST http://127.0.0.1:8000/api/todo[object%20Object] 404 (Not Found) 2dispatchXhrRequest @ xhr.js?b50d:177 3xhrAdapter @ xhr.js?b50d:13 4dispatchRequest @ dispatchRequest.js?5270:52 5Promise.then (async) 6request @ Axios.js?0a06:61 7Axios.<computed> @ Axios.js?0a06:87 8wrap @ bind.js?1d2b:9 9_callee2$ @ App.vue?234e:38 10tryCatch @ runtime.js?96cf:63 11invoke @ runtime.js?96cf:293 12eval @ runtime.js?96cf:118 13asyncGeneratorStep @ asyncToGenerator.js?1da1:3 14_next @ asyncToGenerator.js?1da1:25 15eval @ asyncToGenerator.js?1da1:32 16eval @ asyncToGenerator.js?1da1:21 17insertTodo @ App.vue?234e:38 18invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1854 19invoker @ vue.runtime.esm.js?2b0e:2179 20original._wrapper @ vue.runtime.esm.js?2b0e:6917 21vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v-on handler (Promise/async): "Error: Request failed with status code 404" 22 23found in 24 25---> <App> at src/App.vue 26 <Root> 27warn @ vue.runtime.esm.js?2b0e:619 28logError @ vue.runtime.esm.js?2b0e:1884 29globalHandleError @ vue.runtime.esm.js?2b0e:1879 30handleError @ vue.runtime.esm.js?2b0e:1839 31eval @ vue.runtime.esm.js?2b0e:1856 32Promise.catch (async) 33invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1856 34invoker @ vue.runtime.esm.js?2b0e:2179 35original._wrapper @ vue.runtime.esm.js?2b0e:6917 36vue.runtime.esm.js?2b0e:1888 Error: Request failed with status code 404 37 at createError (createError.js?2d83:16) 38 at settle (settle.js?467f:17) 39 at XMLHttpRequest.handleLoad (xhr.js?b50d:62) 40logError @ vue.runtime.esm.js?2b0e:1888 41globalHandleError @ vue.runtime.esm.js?2b0e:1879 42handleError @ vue.runtime.esm.js?2b0e:1839 43eval @ vue.runtime.esm.js?2b0e:1856 44Promise.catch (async) 45invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1856 46invoker @ vue.runtime.esm.js?2b0e:2179 47original._wrapper @ vue.runtime.esm.js?2b0e:6917

下記:php artisan route:list --path=api/todoを実行

cli

1+--------+-----------+-----------------+--------------+---------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+-----------------+--------------+---------------------------------------------+------------+ | | GET|HEAD | api/todo | todo.index | App\Http\Controllers\TodoController@index | api | | | POST | api/todo | todo.store | App\Http\Controllers\TodoController@store | api | | | GET|HEAD | api/todo/{todo} | todo.show | App\Http\Controllers\TodoController@show | api | | | PUT|PATCH | api/todo/{todo} | todo.update | App\Http\Controllers\TodoController@update | api | | | DELETE | api/todo/{todo} | todo.destroy | App\Http\Controllers\TodoController@destroy | api | +--------+-----------+-----------------+--------------+---------------------------------------------+------------+

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Lulucom

2021/04/06 23:46 編集

例えば、POSTリクエストしている部分を以下のようにするとどうでしょうか。 await axios.post("http://127.0.0.1:8000/api/todo", sendData);
abrt29

2021/04/07 09:20

そのURLで実行しましたがエラー内容が代わり404番のエラーが発生しました。
Lulucom

2021/04/07 10:00

php artisan route:list --path=api/todo コマンドの結果も質問へ掲載可能でしょうか。
abrt29

2021/04/07 11:59

+--------+-----------+-----------------+--------------+---------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+-----------------+--------------+---------------------------------------------+------------+ | | GET|HEAD | api/todo | todo.index | App\Http\Controllers\TodoController@index | api | | | POST | api/todo | todo.store | App\Http\Controllers\TodoController@store | api | | | GET|HEAD | api/todo/{todo} | todo.show | App\Http\Controllers\TodoController@show | api | | | PUT|PATCH | api/todo/{todo} | todo.update | App\Http\Controllers\TodoController@update | api | | | DELETE | api/todo/{todo} | todo.destroy | App\Http\Controllers\TodoController@destroy | api | +--------+-----------+-----------------+--------------+---------------------------------------------+------------+ このように表示されております!
Lulucom

2021/04/07 12:31

ありがとうございます。 質問を後から編集できますので、見やすいように質問へ追記していただければと思います。 todo.storeのルートはちゃんとあるのに404エラーになるのは変ですね・・・ > 404番のエラーが発生しました。 そのとき他にも色々とエラー情報が出力されているかと思うのですが、質問へ掲載可能でしょうか。公開したくないところを隠していただいて大丈夫です。
abrt29

2021/04/07 12:41

問題を修正いたしました。 追記の仕方がLulucomさんが思ってるようなやり方ではないのかもしれませんがご確認お願いします、、、
Lulucom

2021/04/07 12:58

ありがとうございます。質問の編集がまだされていないようなのですが・・・
abrt29

2021/04/07 13:07

申し訳ございません。修正が反映されていませんでした。反映されたと思いますのでご確認お願いします。
Lulucom

2021/04/07 13:10 編集

ありがとうございます。404エラーの方は原因わかりました。「,」(カンマ)にすべきところを「.」(ドット)にしてしまったためではないでしょうか。sendDataの直前はカンマです。修正してみてください。 await axios.post("http://127.0.0.1:8000/api/todo", sendData);
abrt29

2021/04/07 13:32

無事実装したかったこと(追加・更新・削除)が実装できました!親身になって解決してくださり、本当に感謝いたします!ベストアンサーをつけさせていただきたいので回答の方にもう一度ご記入いただけますでしょうか
guest

回答1

0

ベストアンサー

POSTリクエストしている部分を以下のようにするとどうでしょうか。

js

1await axios.post("http://127.0.0.1:8000/api/todo", sendData);

参考: https://github.com/axios/axios#request-method-aliases

投稿2021/04/07 13:34

Lulucom

総合スコア1899

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

abrt29

2021/04/07 14:05

何度も何度もありがとうございました!フォローさせて頂きましたので今後ともよろしくお願いいたします!
Lulucom

2021/04/07 14:05

ありがとうございます^ ^
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問