Py学习  »  Jquery

ASP.net MVC核心Razor页面和Ajax JQuery

redar ismail • 4 年前 • 304 次点击  

非常感谢你的帮助。我从工作中得到了一项任务,我已经有两个星期没有坚持了。非常感谢你的帮助和建议。我正在使用ASP.NET core 2.1和EnitityFramework。 我收到了这个错误:HTTP404:未找到-服务器未找到与请求的URI(统一资源标识符)匹配的任何内容。 (XHR)柱- https://localhost:44336/EducationPage?handler=Add " 我已经试了两个星期了。谢谢你的帮助。 我有一个叫EducationPage的剃须刀。在那个页面中,我正在填充我的数据。在同一个页面中,我希望允许用户通过单击create按钮创建新的教育对象,该按钮将弹出一个表单。用户输入数据,一旦点击add按钮,我的ajax代码就会被触发。 这是我的表格

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">×</button>
            </div>
            <div class="modal-body">
                <form id="myForm" method="post">
                    @Html.AntiForgeryToken()
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <input type="hidden" asp-for="obj.EducationDetailId" id="eduId" />
                    <input type="hidden" asp-for="obj.UserId" id="userId" />
                    <div class="form-group">
                        <label asp-for="obj.InstituteName" class="control-label"></label>
                        <input asp-for="obj.InstituteName" id="instName" class="form-control" />
                        <span asp-validation-for="obj.InstituteName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="obj.CertificateName" class="control-label"></label>
                        <input asp-for="obj.CertificateName" id="certName" class="form-control" />
                        <span asp-validation-for="obj.CertificateName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="obj.Major" class="control-label"></label>
                        <input asp-for="obj.Major" id="major" class="form-control" />
                        <span asp-validation-for="obj.Major" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <label asp-for="obj.startDate" class="control-label"></label>
                        <input type="text" id="startDate" asp-format="{0:MM/dd/yyyy}" asp-for="obj.startDate" class="form-control" />
                        <span asp-validation-for="obj.startDate" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="obj.EndDate" class="control-label"></label>
                        <input type="text" id="endDate" asp-format="{0:MM/dd/yyyy}" asp-for="obj.startDate" class="form-control" />
                        <span asp-validation-for="obj.EndDate" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="obj.Grade" class="control-label"></label>
                        <input asp-for="obj.Grade" id="grade" class="form-control" />
                        <span asp-validation-for="obj.Grade" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="obj.EduDescrp" class="control-label"></label>
                        <textarea asp-for="obj.EduDescrp" id="descp" class="form-control"></textarea>
                        <span asp-validation-for="obj.EduDescrp" class="text-danger"></span>
                    </div>
                    @*<div class="form-group">
                            <label asp-for="obj.UserId" class="control-label"></label>
                            <select asp-for="obj.UserId" class ="form-control" asp-items="ViewBag.UserId" hidden></select>
                        </div>*@
                    @*<div class="form-group">
                            <input type="submit" value="Create" class="btn btn-primary" />
                        </div>*@
                </form>
                <div id="msg"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="btnAdd">Add</button>
                <button type="button" class="btn btn-primary" id="btnUpdate" style="display:none;" onclick="Update();">Update</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

我的Ajax代码是这样的:

<script>
        $("#btnAdd").click(function () {
            var options = {};
            options.url = "/EducationPage?handler=Add";
            options.type = "POST";

            var obj = {};
            obj.eduId = $("#eduId");
            obj.userId = $("#userId");
            obj.instName = $("#instName").val();
            obj.certName = $("#certName").val();
            obj.major = $("#major").val();
            obj.startDate = $("#startDate").val();
            obj.endtDate = $("#endDate").val();
            obj.descrp = $("#descp").val();
            console.log(obj);
            options.data = JSON.stringify(obj);
            options.data = $("#myForm").serialize;
            //console.log(options.data)
            options.contentType = "application/json; charset=utf-8";
            options.dataType = "json";
            options.beforeSend = function (xhr) {
            xhr.setRequestHeader("MY-XSRF-TOKEN",
            $('input:hidden[name="__RequestVerificationToken"]').val());
            };
            options.success = function (msg) {
                $("#msg").html(msg);
            };
            options.error = function () {
                $("#msg").html("Error while making Ajax call!");
            };
            console.log(options);
            $.ajax(options);
        });
    </script>

我的后端是代码是这个

   [HttpPost]
        public async Task<IActionResult> OnPostAdd([FromBody] Obj obj)
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            var UserInDB = await _userManager.GetUserAsync(HttpContext.User);
            obj.UId = UserInDB.Id;
            _db.Obj.Add(obj);
            await _db.SaveChangesAsync();
            return new JsonResult("Customer Added Successfully!");
        }



    The error message I am getting is this :
HTTP404: NOT FOUND - The server has not found anything matching the       requested URI (Uniform Resource Identifier).
    "(XHR)POST - https://localhost:44336/EducationPage?handler=Add"
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52689
 
304 次点击  
文章 [ 1 ]  |  最新文章 4 年前
redar ismail
Reply   •   1 楼
redar ismail    5 年前

它现在工作了,我的错,我在我的服务中偶然给了一个空间。AddAntiforgery(option=> option.HeaderName=“MY-XSRF-TOKEN”);我删除了它,它工作了。所以上面的代码没什么问题