Postman中POST请求正常,Vue+Axios调用返回404错误求助
Let's break down the core issue first: your Postman request works because it passes quad and password as URL query parameters, but your Axios code sends them in the JSON request body. Since your backend responds successfully to one but not the other, the problem lies in how your ASP.NET Core endpoint expects to receive parameters. Here's how to fix it:
1. Match Postman's Parameter Style in Axios
If you want to keep your backend as-is, adjust your Axios request to send parameters as query strings (just like Postman):
axios({ method: 'post', url: 'http://localhost:64427/api/Authenticate/Token', // Use `params` instead of `data` to pass query parameters params: { quad: this.quad, password: this.password } }) .then(response => { // Quick fix: Axios uses `response.headers` (plural), not `response.header` console.log(response.headers.token); }) .catch(error => { console.error('Request failed:', error); });
Note: Your original code had a small typo — response.header.token should be response.headers.token (Axios exposes response headers as a plural property).
2. Update Backend to Accept JSON Body (Recommended for POST)
POST requests are designed to send data in the request body (not query strings) for security and best practices. To make your backend work with your original Axios code:
Step 1: Create a request model in your backend
public class LoginCredentials { public string Quad { get; set; } public string Password { get; set; } }
Step 2: Modify your Action to bind from the JSON body
Update your Token endpoint to explicitly read parameters from the request body using [FromBody]:
[HttpPost("Token")] public IActionResult Token([FromBody] LoginCredentials credentials) { if (credentials == null || string.IsNullOrEmpty(credentials.Quad) || string.IsNullOrEmpty(credentials.Password)) { return BadRequest("Invalid login credentials"); } // Your authentication logic here }
With this change, your original Axios code will work as-is — Axios automatically sets Content-Type: application/json when sending a JavaScript object in the data field.
Extra Checks to Rule Out Other Issues
- Verify Route Configuration: Make sure your
AuthenticateControllerhas the correct route attribute, e.g.,[Route("api/[controller]")]— this ensures the full endpoint URLapi/Authenticate/Tokenmatches what you're calling in Axios. - Inspect Browser Network Tab: Check the actual request sent by Axios in your browser's DevTools Network tab. Confirm the URL, HTTP method (POST), and payload/query parameters are correct.
- CORS Setup: While a 404 isn't a CORS error, if your frontend runs on a different port than the backend, ensure your ASP.NET Core app has CORS enabled to avoid blocked requests.
内容的提问来源于stack exchange,提问作者Aatish Kumar

