Laravel中AJAX调用出现404错误请求问题求助
Hey there! Let's walk through fixing that 404 error you're hitting when trying to access the Facebook login endpoint. Here are the key things to check step by step:
1. Verify Your BASE_PATH Value
First up, double-check what BASE_PATH is actually set to in your frontend code. Your request is going to http://localhost/Script/login/fb, which means BASE_PATH is likely http://localhost/Script.
You need to confirm if your Laravel app's public directory is mapped to the /Script subfolder on your server. If Laravel is actually running at the root (http://localhost), then BASE_PATH should be set to that instead—otherwise you're sending requests to a subfolder that doesn't align with your Laravel routes.
Quick test: Open your browser's dev tools console, run console.log(BASE_PATH), and check the output to make sure it matches your Laravel app's base URL.
2. Confirm the Route is Registered
Next, let's make sure your route is actually being picked up by Laravel. Run this command in your terminal:
php artisan route:list
Look for the /login/fb GET route in the output. It should show the name loginWithFb and point to UserController@loginWithFb.
If it's not there, a few possible issues:
- You might have a typo in your
routes/web.phpfile (check capitalization, slashes, etc.) - Another route above it might be conflicting (for example, a wildcard route like
Route::get('/login/{provider}', ...)that's catching the request first)
3. Check the Controller and Method Exist
Make sure your UserController is in the correct location (app/Http/Controllers/UserController.php) and that it has a public loginWithFb method. Here's a quick template to verify:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function loginWithFb() { // Your Facebook login logic will go here // For testing, you could return a simple response first: return response()->json(['user' => ['name' => 'Test User']]); } }
If the method is missing, private, or the controller doesn't exist, Laravel will throw a 404 (or a 500 error, but sometimes it manifests as 404 if routing can't resolve the controller).
4. Fix Server Configuration (If Laravel is in a Subfolder)
Since your request is going to /Script/login/fb, it looks like Laravel is running in a subfolder. If you're using Apache, update your public/.htaccess file to set the correct RewriteBase:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Send Requests To Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f # Update this line to match your subfolder RewriteRule ^ /Script/index.php [L] </IfModule>
If you're using Nginx, adjust your server block to include the subfolder in the try_files directive:
location / { try_files $uri $uri/ /Script/index.php?$query_string; }
5. Clear Route Cache
If you've ever run php artisan route:cache in the past, your routes might be cached—meaning any recent changes to routes/web.php won't take effect until you clear the cache. Run this command to fix that:
php artisan route:clear
Start with checking BASE_PATH and running route:list—those are the most common culprits for this kind of 404. Let me know if any of these steps get you up and running!
内容的提问来源于stack exchange,提问作者user3653474

