Windows Server2016下IIS10网站API DELETE方法报405错误求助
Hey there! Let's figure out why your DELETE method isn't firing correctly—since you've already eliminated WebDAV as a culprit (great call checking that first), let's walk through other common fixes tailored to your Windows Server setup:
1. Verify IIS Allows the DELETE Method
Windows Server's IIS often blocks non-standard HTTP methods by default. Here's how to check:
- Open IIS Manager, navigate to your website
- Go to Request Filtering → HTTP Methods tab
- If DELETE isn't listed, click Allow Verb and add "DELETE" to the list
- Restart your IIS site to apply changes
2. Double-Check Your PHP DELETE Handling Logic
From the snippet you shared, make sure you're properly handling the DELETE request in your code. Unlike POST, DELETE data doesn't populate $_POST—you'll need to fetch it from the request body or URL parameters. Here's a quick example of how to structure this:
if ($_SERVER['REQUEST_METHOD'] == "DELETE") { // Get ID from URL parameter (adjust based on your routing) $recordId = $_GET['id'] ?? null; // Or parse JSON body if you're sending data that way $requestBody = json_decode(file_get_contents('php://input'), true); $recordId = $requestBody['id'] ?? $recordId; if ($recordId) { // Execute your delete query using the DB class $deleteSuccess = $db->query("DELETE FROM your_table WHERE id = ?", [$recordId]); if ($deleteSuccess) { http_response_code(200); echo json_encode(['status' => 'success', 'message' => 'Record deleted']); } else { http_response_code(500); echo json_encode(['status' => 'error', 'message' => 'Failed to delete record']); } } else { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Missing record ID']); } }
3. Fix URL Rewrite Rules (If Using Friendly URLs)
If your API uses clean URLs (e.g., /api/users/123 instead of index.php?id=123), ensure your IIS rewrite rules don't block DELETE requests. Update your web.config to explicitly allow DELETE:
<rewrite> <rules> <rule name="API Rewrite" stopProcessing="true"> <match url="^api/(.*)$" /> <action type="Rewrite" url="index.php?route={R:1}" /> <conditions> <!-- Allow GET, POST, and DELETE methods --> <add input="{REQUEST_METHOD}" pattern="GET|POST|DELETE" /> </conditions> </rule> </rules> </rewrite>
4. Test with Proper Tools
Browsers only support GET and POST by default—so testing DELETE directly in a browser won't work. Use tools like Postman, Insomnia, or curl to send a proper DELETE request:
# Example curl command to test curl -X DELETE "http://your-server-url/api/records/456"
5. Check Firewall/Security Software
Windows Firewall or third-party security tools on your server might be blocking DELETE requests. Verify that incoming requests on port 80/443 allow the DELETE method.
If none of these steps fix the issue, share your full DELETE handling code and any routing setup you have—we can dig deeper from there!
内容的提问来源于stack exchange,提问作者Daan Hermans

