【问题标题】:404 error when making ajax call to asp.net MVC controller对 asp.net MVC 控制器进行 ajax 调用时出现 404 错误
【发布时间】:2017-01-24 02:51:32
【问题描述】:

我正在尝试向 MVC 控制器发出 ajax 请求,但每次尝试执行我的脚本时都会收到 404 错误。

Failed to load resource: the server responded with a status of 404 () https://localhost:44301/LocationMapping/Test

我的请求只是从下面的代码 sn-p 返回error get


我的项目中相关文件的结构是

查看调用请求的位置 -> courselister/index.cshtml

视图的控制器 -> CourseListerController.cs

上面视图中处理Ajax请求的我的控制器-> LocationMappingController.cs


我处理来自不同控制器的请求的原因是因为多个视图将使用相同的请求,因此将这些请求放在一起似乎更好 LocationMappingController

这是我的 ajax 请求的代码

$(document).ready(function () {
    $.ajax({
            url: "/LocationMapping/Test",
            type: "GET",
            dataType: "json",
            data: {},
            error: function (error) {
                alert('error get');
            },
            success: function (data) {
                alert('GET success');
                alert(data);
            }
        });
});

这是 LocationMapping 控制器中的代码

public JsonResult Test() {
{
    return Json("Get Test", JsonRequestBehavior.AllowGet);
}

我目前的理论是服务器找不到路由/LocationMapping/Test,因为它不是视图的控制器(CourseListerController.cs)。我已经花了很长时间才找到解决方案,因此我向 SO 社区寻求帮助。

感谢任何帮助。


更新:我尝试创建一个新的控制器和一个与之关联的视图,如下所示,

TestAjaxController.cs

public ActionResult Index()
{
    return View();
}

public JsonResult GetResponse(string str)
{
    return Json(str, JsonRequestBehavior.AllowGet);
}

TestAjaxController/Index.cshtml

<div> 
    <h1>Test Ajax Page: Location Mapping</h1>
    <button onclick="callAjax()">Click Me</button>
    <script>
        //document.ready(function () {
            console.log('jquery loaded');
            function callAjax() {
                $(document).ready(function () {
                    alert("called");
                    jQuery.ajax({
                        url: "/TestAjax/GetResponse",
                        type: "GET",
                        dataType: "json",
                        data: { str: "this is the string I want to get as a response" },
                        error: function () {
                            alert("Error occurred calling GetResponse");
                        },
                        success: function (data) {
                            alert(data);
                        }
                    });
                });         
            }            
    </script>
</div>

这行得通!我收到“这是我想要作为响应的字符串”的提示。我不明白为什么当我的其他控制器不起作用时它会起作用..

【问题讨论】:

  • 您已确认您的服务器正在侦听端口 44301?你能从那个端口上的服务器得到一个 ACK​​ 吗?
  • 尝试用url: '@Url.Action("Test","LocationMapping")',代替url: "/LocationMapping/Test",,结果一样吗?
  • 您不必在您的 URL 中调用您的端口号吗?对我来说已经很久了......
  • 我在使用@Url 建议时仍然收到相同的 404 状态 :(
  • 地址栏输入/LocationMapping/Test会发生什么?你在使用区域吗?

标签: javascript c# asp.net ajax asp.net-mvc


【解决方案1】:

面临同样的问题。关闭浏览器中的本地主机选项卡并在没有调试的情况下运行启动。我能够查看路由中的 JSON 数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 2014-11-10
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多