【问题标题】:ASP.NET MVC 3 AJAX request returns 404 Not Found errorASP.NET MVC 3 AJAX 请求返回 404 Not Found 错误
【发布时间】:2012-08-10 12:06:31
【问题描述】:

这是我的代码(问题如下):

查看

// This function is called by another function when radioButtonGroup.change().
var requestValues = function (form) {
    var option = form.find("input:radio:checked").attr("value");

    // This seemingly shows the correct url for the action method desired.
    alert("Form Action: " + form[0].action + "\nForm Method: " + form[0].method);

    if (form.valid()) {
        $.ajax({
            url: form[0].action,
            type: form[0].method,
            data: option,
            success: function (result) {
                alert("Had success.");
                $('#createForm').replaceWith(result);
            },
            error: function (xhr) {
                alert("An error occurred: " + xhr.status + " " + xhr.statusText);
            }
        });
    }
    return false;
}

...(other code here)...

@using (Html.BeginForm("CreateForm", "MyController", FormMethod.Post, 
                        new { @id = "optionForm" }))
{
    <div id="options">
        @foreach (MyOption op in Model.GetOptions()) {
            <div class="editor-field">
            @Html.RadioButton("formOption", op.OptionType, false, 
                new { @id = op.ID, @title = @op.Description })
            <label for="@op.ID">@op.Name</label>
            </div>
        }
    </div>
    <input type="submit" value="Select" style="display:none;" />
}

控制器

[HttpPost]
public PartialViewResult CreateForm(MyOptionType formOption) {
    MyViewModel model = new MyViewModel();
    model.ApplyOptionValues(formOption);
    return PartialView("_CreateForm", model);
}

注册路线

// Default
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

我的问题是,当我单击单选按钮时,AJAX 请求会执行,但出现“404 Not Found”错误(即使 jQuery 函数中的 alert 似乎显示了适当的 url)。我昨天花了一整天的时间在这上面,我不知道到底出了什么问题。我在 IIS Express 上运行 ASP.NET MVC 3 应用程序,但我没有使用区域(无论如何我都知道)。有人对如何解决这个问题有任何建议吗?谢谢。

编辑

警告框显示以下消息:

表单操作:https://localhost:44300/MyController/CreateForm

表单方法:post

编辑

这是重新创建错误的完整测试视图和测试控制器:

查看

<h2>TestAction</h2>

<script type="text/javascript">
    $(document).ready(function () {
        $("#optionForm input[name='radioOption']").change(function () {
            requestValues($(this).closest("form"));
        });

        var requestValues = function (form) {
            var option = form.find("input:radio:checked").attr("value");

            alert("Form Action: " + form[0].action + "\nForm Method: " + form[0].method);

            if (form.valid()) {
                $.ajax({
                    url: form[0].action,
                    type: form[0].method,
                    data: option,
                    success: function (result) {
                        alert("AJAX success.");
                        //$('#createForm').replaceWith(result);
                    },
                    error: function (xhr) {
                        alert("An error occurred: " + xhr.status + " " + xhr.statusText);
                    }
                });
            }
            return false;
        }
    });
</script>

@using (Html.BeginForm("CreateForm", "Test", FormMethod.Post, new { @id = "optionForm" })) {
    @Html.RadioButton("radioOption", "value1", false, new { @id = "radioButton1" })
    <label for="radioButton1">Radio Button 1</label>
    @Html.RadioButton("radioOption", "value2", false, new { @id = "radioButton2" })
    <label for="radioButton2">Radio Button 2</label>
    @Html.RadioButton("radioOption", "value3", false, new { @id = "radioButton3" })
    <label for="radioButton3">Radio Button 3</label>

    <input type="submit" value="Select" style="display:none;" />
}

<div id="createForm"></div>

控制器

public class TestController : Controller {
    public ActionResult TestAction() {
        return View();
    }

    [HttpPost]
    public ActionResult CreateForm(string option) {
        return View("TestAction");
    }
}

【问题讨论】:

  • 建议您使用 Fiddler 之类的工具,或 Chrome 中的网络工具(Ctrl Shift I) - 404 将以红色突出显示,您可以看到有问题的 URL 是什么。
  • MyController是控制器的真名吗?你能告诉我们更多细节来尝试重现你的问题吗?
  • 您能发布警报中显示的内容吗?
  • 您确定使用 https 和端口 44300 吗?我猜你的 iis express 配置错误。
  • @nemesv,我已在编辑中添加了警报消息。

标签: asp.net-mvc asp.net-mvc-3 jquery asp.net-ajax http-status-code-404


【解决方案1】:
@using (Html.BeginForm("CreateForm", "MyController", FormMethod.Post, new { id = "optionForm" }))

应该是:

@using (Html.BeginForm("CreateForm", "My", FormMethod.Post, new { id = "optionForm" }))

请记住,在 ASP.NET MVC 帮助程序中,您不应传递 Controller 后缀。假设。

所以正确的网址应该是:

https://localhost:44300/My/CreateForm

而不是:

https://localhost:44300/MyController/CreateForm

你显然有 MyController 类:

public class MyController: Controller
{
    public ActionResult CreateForm(MyOptionType formOption)
    {
        ...
    }
}

【讨论】:

  • “MyController”示例名称是一个糟糕的选择。正如您提到的,真正的代码省略了“控制器”部分。我发布了新的测试代码,可能会更好地显示它。对于造成的混乱,我深表歉意。
  • data: option 应该是 data: { option: option }。另外你在哪里看到这个 404 错误?在 FireBug 中查看 AJAX 请求时?此外,您似乎正在使用 HTTPS。您确定这在 IIS Express 中已正确配置吗?不要忘记 IIS Express 中的 HTTPS 端口与用于服务 HTTP 的端口不同。
  • 我会尝试更改数据,但我确实已经从 ajax 代码中删除了数据,并从被调用的操作方法中删除了参数,并且生成了相同的错误。现在,关于 404 错误,我在 ajax 代码的 error: ... 部分的警告框中看到了它。 Https ...我以为我已经准备好使用它了,因为我所有的其他控制器/动作组合都在 https 上工作,并且在尝试使用 ajax 之前我没有遇到任何问题。
  • 好吧,这很奇怪。我只是将控制器操作上的HttpPost attr 更改为HttpGet,然后我得到了成功警报框(来自我上面的测试代码)......我不再得到错误。但是,我没有更改 HTML 表单的FormMethod,所以仍然是FormMethod.Post。到底是怎么回事?哦,顺便说一句,...Test/TestAction 总是有效的。是 ...Test/CreateForm 给出了 404 错误,正如我所提到的,尽管 POST/GET 不一致,两者现在都可以工作。
  • 您可以使用(不带双引号)"type: 'POST'" 来强制 ajax 请求为 POST。 jQuery 默认为 GET,除非您有全局设置(请参阅api.jquery.com/jQuery.ajaxSetup)。但是,在请求中指定应该覆盖全局/默认值。认为您需要做的第一件事是确认您是否真的在使用上述技术进行 GET/POST。然后,您可以将任何模型绑定错误作为一个单独的问题来担心!
猜你喜欢
  • 2020-03-16
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2011-08-03
相关资源
最近更新 更多