所以我遇到了一个奇怪的问题,即如果我通过Vue中的axios发出Put请求,则会将其发送到错误的路由。
它是通过Laravel API资源进行的简单CRUD的一部分。获取并发布工作正常。
这是表格:
<form @submit.prevent="editType" method="post" class="mb-4">
<label v-if="user.gender === 'man'" for="single_male">Enkel<input type="checkbox" name="type" :id="single_male" :value="types.single_male" v-model="checkedType" @change="showSaveButton = true"></label>
<label v-if="user.gender === 'vrouw'" for="single_female">Enkel<input type="checkbox" name="type" :id="single_female" :value="types.single_female" v-model="checkedType" @change="showSaveButton = true"></label>
<label v-if="user.gender === 'man'" for="double_male">Dubbel mannen<input type="checkbox" name="type" :id="double_male" :value="types.double_male" v-model="checkedType" @change="showSaveButton = true"></label>
<label v-if="user.gender === 'vrouw'" for="double_female">Dubbel vrouwen<input type="checkbox" name="type" :id="double_female" :value="types.double_female" v-model="checkedType" @change="showSaveButton = true"></label>
<label for="double_mix">Dubbel gemengd<input type="checkbox" name="type" :id="double_mix" :value="types.double_mix" v-model="checkedType" @change="showSaveButton"></label>
<button type="submit" v-if="showSaveButton">Opslaan</button>
</form>
我的更新功能:
public function update(Request $request, $id)
{
$user_id = auth('api')->user()->id;
$type = Type::where('user_id' ,'=', $user_id)->first();
$type->single_male = $request->input('single_male');
$type->single_female = $request->input('single_female');
$type->double_male = $request->input('double_male');
$type->double_female = $request->input('double_female');
$type->double_mix = $request->input('double_mix');
$type->save();
return redirect()->back();
}
我的方法:
editType() {
let types = this.types;
let data = {
single_male: this.single_male,
single_female: this.single_female,
double_male: this.double_male,
double_female: this.double_female,
double_mix: this.double_mix,
};
axios.put('/types/'+types.id, data)
.then(request => this.successfulEdit(request))
.catch(() => this.failed())
},
successfulEdit() {
alert("Voorkeuren succesvol bijgewerkt!");
}
和我的路线:
Route::apiresource('types','TypeController');
当我发出放置请求时,我可以在开发人员工具中看到它尝试将请求发送到当前网址,这当然会导致405方法不允许错误。 当我将axios请求更改为just('/ types',data)时,它确实遵循了指定的路由,但由于put方法需要一个id,因此当然也会出现405错误。硬编码为“ / types / 4 /”也会导致使用当前网址。
我错过了什么吗?怎么了?预先感谢大家!
答案 0 :(得分:1)
发现了问题。 onPause
是罪魁祸首。谢谢大家!
答案 1 :(得分:0)
来自Laravel文档。 https://laravel.com/docs/5.8/routing
routes / api.php文件中定义的路由嵌套在路由中 由RouteServiceProvider分组。在该组中,/ api URI 前缀会自动应用
因此,请尝试在您当前的请求网址之前添加/api
,例如
axios.put('/api/types/'+types.id, data)