0

Thank you so much for your help. I got a task from the work and it has been two weeks since I am stuck with. Your help and advice is much appreciated. I am using ASP.NET core 2.1 and EnitityFramework. I am getting this error ":HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (XHR)POST - https://localhost:44336/EducationPage?handler=Add" I have been trying now for two weeks. Thank you for your help. I have a single RazorPage called EducationPage. Inside that page I am populating my data. In the same page I want to allow user to create new Education object by clicking on Create button which will pop up a form. User Enters the data, and once it hits the add button my ajax code will be fired. Here is my form

<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>

My Ajax Code is like that :

<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>

My back end is code is this

   [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"
2
  • options.data = JSON.stringify(obj); at this line, did you try asigning just obj variable to data? like this : options.data = obj; Commented Mar 14, 2019 at 6:55
  • This http error message is not related to razor page. Is EducationPage your controller or action? Your action is OnPostAdd. But your url is like /EducationPage... Are you sure this is correct. I'm asking because of I do not know the sturcture of your project. If EducationPage is your controller, try to change the url like this /EducationPage/OnPostAdd... Commented Mar 14, 2019 at 7:00

1 Answer 1

1

It is working now, My Bad, I gave accediently a space inside my services.AddAntiforgery(option => option.HeaderName = "MY-XSRF-TOKEN"); which I removed and it works. So nothing bad with code above

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.