2

我有一个带有提交按钮的 ajax 表单,但是当我单击提交按钮时,我得到一个 404 page not found 错误。

我的部分观点:

@using (Ajax.BeginForm("Index", "Maps", null, ajaxOptions, new { @class = "form-search" }))
{
    ...
    ...
    <button type="submit" class="btn">Search</button>
}

ajaxOptions 变量定义如下:

var ajaxOptions = new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "filter-results",
    LoadingElementId = "filter-loading",
    LoadingElementDuration = 2000,
    OnFailure = "showAjaxError",
    Url = Url.Action("Index")
};

这在我的浏览器(Chrome)中呈现如下:

<form action="/Maps" class="form-search" data-ajax="true" data-ajax-failure="showAjaxError" data-ajax-loading="#filter-loading" data-ajax-loading-duration="2000" data-ajax-mode="replace" data-ajax-update="#filter-results" data-ajax-url="/Maps" id="form0" method="post">    <div class="well well-small">
        <div class="input-append">
            <input type="text" class="span3">
            <button type="submit" class="btn">Search</button>
        </div>
        <div class="input-prepend pull-right">
            <button type="submit" class="btn">Items Per Page</button>
            <input type="number" name="ItemsPerPage" class="span1" value="10">
        </div>
    </div>
</form>

404 错误说:

Requested URL: /Maps

我可以将它粘贴到我的浏览器中,它可以工作:

http://localhost:7374/Maps

我想我引用了正确的 javascript 文件 - 查看页面源代码,我看到了这些:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.0/jquery-ui.min.js"></script>

在我的 Web.config 我有这个:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
4

3 回答 3

3
  1. 检查控制器是否MapsControllerIndex接受HttpGet。因为你用POST方法发送
  2. 你需要这个Url = Url.Action("Index")吗?
  3. 看到这个问题,jQuery 1.9 版本没有.live方法,并且文件jquery.unobtrusive-ajax.js使用这个方法。如果你需要使用 jQuery 1.9+,你必须改变它。

第 3 点的西班牙解决方案(链接)。

于 2013-02-13T22:34:14.237 回答
0

也许您的控制器操作标有 HttpGetAttribute?据我所知,如果您使用 Ajax 助手,您还应该添加对 microsoft ajax 脚本的引用:MicrosoftAjax.js、MicrosoftMvcAjax.js 和 MicrosoftMvcValidation.js

于 2013-02-13T19:36:41.010 回答
0

由于某种原因,这不适用于 jquery 1.9.01.9.1,但适用于以前的版本。

作品:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

不起作用(404 错误):

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
于 2013-02-13T21:39:42.213 回答