You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET中AJAX发起HttpPost请求返回404 Not Found问题求助

Hey there! Let's work through that 404 Not Found error you're hitting when calling your ASP.NET API via AJAX POST—this is a super common issue, so we'll break down the most likely culprits step by step.

1. Double-check your controller's routing setup

First, make sure your controller and action are properly annotated with routing attributes:

  • Does your controller have the [ApiController] attribute? This enables key API features like automatic model validation and routing conventions.
  • Verify the [Route] attribute on your controller—for example, if it's [Route("api/[controller]")], your base URL will be /api/YourControllerName (note: ASP.NET strips the "Controller" suffix from the class name, so UserController becomes User in the route).
  • Ensure your POST action has the [HttpPost] attribute, and if you added a specific route template (like [HttpPost("save-data")]), your AJAX URL needs to match that suffix exactly.

Also, don't forget: your controller class and action method must be public—ASP.NET can't route to private/internal methods!

2. Verify your AJAX request URL is correct

It's easy to slip up with relative paths or typos here:

  • Use an absolute path (starting with /) for your AJAX URL to avoid issues with page hierarchy (e.g., /api/Data/SaveData instead of api/Data/SaveData).
  • Open your browser's DevTools (Network tab) to see the exact URL being requested—this will tell you if the path is misspelled or pointing to the wrong location.

3. Match your request data format to the backend

Mismatched data format is another frequent cause of 404 (or bad request) errors:

  • Set contentType: "application/json" in your AJAX call—this tells ASP.NET you're sending JSON data.
  • Use JSON.stringify() to convert your JavaScript object to a JSON string before sending it (raw objects won't be parsed correctly by the API).
  • If your action uses a model class, ensure the property names in your JavaScript object match the model's property names (case sensitivity matters unless you've configured JSON serialization to ignore it). For simple type parameters (like int id), add the [FromBody] attribute to the parameter to tell ASP.NET to read it from the request body.

4. Confirm your ASP.NET routing is enabled

Check your startup configuration (either Program.cs or Startup.cs):

  • For minimal APIs or controllers, make sure you've added app.MapControllers(); to your pipeline—this enables attribute routing for controllers.
  • If you're using traditional MVC, ensure app.UseMvc(); or app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); is present.

5. Test with a tool like Postman first

To rule out frontend issues, send a POST request directly to your API endpoint using Postman or curl. If you still get a 404, the problem is definitely in your backend routing or controller setup. If it works, then the issue is in your AJAX code (likely URL or data format).

Example of working code for reference

Here's a quick example to compare against your setup:

Controller:

[ApiController]
[Route("api/[controller]")]
public class DataController : ControllerBase
{
    [HttpPost("submit")]
    public IActionResult SubmitData([FromBody] PostData model)
    {
        // Your processing logic here
        return Ok(new { message = "Data received successfully!" });
    }
}

public class PostData
{
    public string Username { get; set; }
    public string Email { get; set; }
}

AJAX Call:

const postData = {
    username: "MaxBoy",
    email: "max@example.com"
};

$.ajax({
    url: "/api/Data/submit",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify(postData),
    success: function(response) {
        console.log("Success:", response);
    },
    error: function(xhr, status, error) {
        console.error("Error details:", xhr.responseText);
    }
});

Go through these checks one by one—chances are you'll spot the issue quickly. Let me know if you need to share specific code snippets for further debugging!

内容的提问来源于stack exchange,提问作者Max Boy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:17:09