MVC4 Ajax请求代码404

时间:2014-04-04 18:31:29

标签: jquery ajax asp.net-mvc-4 json.net

每当我在浏览器中检查控制台时,它都说它找不到带有代码404的URL。它执行帖子的URL是http://localhost:54473/Date/GetArticleName

DateController.cs

中的方法
public JsonResult GetArticleName(long barcode)
{
    ArticleDbContext artdb = new ArticleDbContext();
    Article art = (Article) artdb.Articles.Where(a => a.Barcode == barcode).First();
    return Json(art.Name, JsonRequestBehavior.AllowGet);
}

Create.schtml

@model DatoCheckerMvc.Models.Date

@{
    ViewBag.Title = "Opret ny";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Opret</h2>

@using (Html.BeginForm("Create", "Date", FormMethod.Post, new { }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)


    <script type="text/javascript">
        function validationFunction() {
            var isValid = true;
            if (!$("#ArticleBarcode").val() || $("#ArticleBarcode").val() == "" || $("#ArticleBarcode").val().match("^\d+$")) {
                isValid = false;
            }
            if (!$("#RunDate").val() || $("#RunDate").val() == "" || $("#RunDate").val().match("((?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])")) {
                isValid = false;
            }

            if (isValid) {
                return true;
            }
            else {
                return false;
            }
        }

        $(document).ready(function () {
            $(window).keydown(function (event) {
                if ((event.keyCode == 13) && (validationFunction() == false)) {
                    event.preventDefault();
                    return false;
                }
            });

            $("#ArticleBarcode").change(function (event) {
                $.ajax({ //This function is the call to the method.
                    url: '@Url.Action("GetArticleName", "Date")',
                    data: { barcode: $("#ArticleBarcode").val() },
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (result) {
                        var divInsert = document.getElementById("JsonResponse");
                        divInsert.innerHTML = result;
                    },

                    error: function () {

                        alert("error");
                    }
                });
            });
        });
    </script>

    <fieldset>
        <legend>Dato</legend>
        <div class="row">
            <div class="form-group col-md-4">
                <div class="editor-label">
                    @Html.LabelFor(model => model.ArticleBarcode)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.ArticleBarcode, null, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.ArticleBarcode)
                </div>
            </div>
        </div>

        <div class="row">
            <div class="form-group col-md-4">
                <div class="editor-label">
                    <label for="ArticleName">Varenavn</label>
                </div>
                <div class="editor-field">
                    <div id="ArticleName">

                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="form-group col-md-4">
                <div class="editor-label">
                    @Html.LabelFor(model => model.RunDate)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.RunDate, null, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.RunDate)
                </div>
            </div>
        </div>
        <p>
            <input class="btn btn-default" type="submit" value="Opret" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Tilbage", "Index")
</div>


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我有:

  • 确保JQuery在顶部加载
  • 尝试使用$.getJson()代替
  • 在方法中插入断点以查看它是否已执行(它没有执行)

如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

尝试使用HttpPost属性标记您的方法:

[HttpPost]
public JsonResult GetArticleName(long barcode) {
    ...
}

如果没有该属性,则假定它是GET请求。

此外,JsonRequestBehavior.AllowGet如果要成为POST请求,则无需{{1}}。

相关问题