英文:
Post request return 404 in laravel
问题
我编写了两个API。一个是获取函数,另一个是提交函数。
此外,我尝试了以下代码:
public function one(Request $request){
dd("test"); }
public function two(){
dd("test");
}
始终可以正常运行two函数,但one函数无法正常工作。我使用的是http://127.0.0.1:8000。
而我的api.php如下:
Route::post('/one', [\App\Http\Controllers\TestController::class, 'one']);
Route::get('/two', [\App\Http\Controllers\TestController::class, 'two']);
one函数始终返回404未找到错误。如何访问此服务?
英文:
I write two api. One get function and one post function.
Also, I try
public function one(Request $request){
dd("test"); }
public function two(){
dd("test");
}
Always two is working but one does not working. I use http://127.0.0.1:8000.
And my api.php like
Route::post('/one', [\App\Http\Controllers\TestController::class, 'one']);
Route::get('/two', [\App\Http\Controllers\TestController::class, 'two']);
One always return 404 not found. How can I access this service?
答案1
得分: 1
尝试清除路由缓存。
php artisan route:clear
英文:
Try clearing the cache of routes.
php artisan route:clear
答案2
得分: 0
代码看起来正确。
您可以告诉我您的 Web 服务器是什么(例如 Apache/Nginx)吗?
您是否已激活了重写模块?
或者,如果您正在使用 Laravel 并使用 php artisan serve
启动服务器,在编辑或更改代码后,您应该重新启动它。
或者像 @fuadps 所说的那样,“您确定是通过 POST 请求访问 /one 吗?”
英文:
the code looks correct.
Could you please let me know your web server (e.g. Apache/Nginx)?
Have you activated the rewrite module?
Alternatively, if you are using Laravel and starting the server with php artisan serve
, you should restart it after editing or changing the code.
or like @fuadps said "are you sure you are access /one by POST request?"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论