我的路线API:
Route::get('/outline/{key}',"OutlineController@show");
Route::post('/outline',"OutlineController@store");
Route::delete('/outline/{key}',"OutlineController@destroy");
Route::put('/outline/{key}',"OutlineController@update");
我的控制器代码是:
public function update(Request $request ,$key)
{
$request->validate([
'title' => 'string|required',
'content' => 'string|required',
'pseudo' => 'string',
'email' => 'email|required'
]);
try {
if($outline = Outline::where('key',$key)->first()){
$outline = Outline::where('key',$key)->first();
$outline->title = $request->title;
$outline->content = $request->content;
$outline->pseudo = $request->pseudo;
$outline->email = $request->email;
$outline->save()->refresh();
return response()->json([
'message' => "Success!",
'outline' => $outline
],200);
} else {
return response()->json([
'message' => "Not found!"
],404);
}
} catch (\Exception $e){
report($e);
return response()->json([
'message' => "Something went really wrong!"
],500);
}
}
此页面显示404未找到:
我已经尝试了3个小时,但没有反应。请解决这个问题...
我尝试了put和patch方法,但是找不到404页面Show ..
高级谢谢您的帮助.....
答案 0 :(得分:2)
在请求的标题标签中,确保已设置以下标题。这将告诉Laravel您正在发送Ajax请求,而不是标准HTTP请求。
Content-Type: application/json
Accept: application/json
答案 1 :(得分:0)
首先,解决问题的第一部分,例如检查您是否可以打路线。因此,如果您不编写测试,只需在update上使用dd()即可。
public function update(Request $request ,$key)
{
dd($request, $key);
}
如果您打不开路线,请尝试使用邮递员或失眠之类的简单休息客户来创建请求或类似这样的简单表格;
// if you using routes/api.php /api/outline/1234
<form action="/outline/1234" method="POST">
@csrf
@method('PUT')
...
</form>
如果您选择了路线,那么您的问题就出在这里;
return response()->json([
'message' => "Not found!"
],404);