Laravel+Vue+Axios POST请求异常:服务器实际发GET报405错误
Hey there, let's figure out why your POST request is getting turned into a GET in production—super frustrating when local works fine but production throws a curveball! Here are the most likely causes and fixes to try:
1. The Culprit: Trailing Slash + 301 Redirect
This is the most common reason for this exact issue. Look at your axios call: you're sending a POST to /api/properties/countries/ (with a trailing slash).
Laravel's default .htaccess has a rule that redirects URLs with trailing slashes to the non-trailing-slash version (301 permanent redirect). When browsers handle a 301 redirect for a POST request, they automatically convert it to a GET request—boom, that's your 405 error right there.
Fix:
Update your axios call to remove the trailing slash:
axios.post('/api/properties/countries') // No trailing slash here
If you want to keep the trailing slash for some reason, you can temporarily comment out the trailing slash redirect rule in your .htaccess to test (though this isn't ideal for SEO long-term):
# Redirect Trailing Slashes If Not A Folder... # RewriteCond %{REQUEST_FILENAME} !-d # RewriteCond %{REQUEST_URI} (.+)/$ # RewriteRule ^ %1 [L,R=301]
2. Verify .htaccess is Working in Plesk
Plesk sometimes restricts .htaccess overrides by default, which can break Laravel's routing.
Steps to check:
- Log into Plesk, go to your domain's Apache & nginx Settings
- Under Apache directives, make sure
AllowOverride AllorAllowOverride FileInfois set (this lets.htaccessrules take effect) - Double-check that your production server has the correct Laravel
.htaccessfile in the root directory (the same one that works locally)
3. Clear Route Cache (If Applicable)
If you ran php artisan route:cache in production after setting up your API routes, any later changes to routes/api.php won't take effect unless you clear the cache.
Run these commands on your VPS:
php artisan route:clear # If you want to re-cache routes after clearing php artisan route:cache
4. Rule Out Browser Cache
Sometimes old cached JS or request data can cause weird behavior. Test your app in incognito mode, or do a hard refresh (Ctrl+Shift+R) to make sure you're loading the latest version of your app.js.
5. Check Plesk Redirect Rules
Make sure there aren't any custom redirects set up in Plesk (under Redirects for your domain) that might be interfering with your API path. Even a simple redirect for /api could mess with request methods.
Remember, since manually sending a POST to the endpoint works, the server is correctly configured to handle POST requests—this is almost definitely a redirection or client-side URL issue. Start with fixing the trailing slash, that's the most probable fix here!
内容的提问来源于stack exchange,提问作者Maramal

