POST method not working in .NET Core MVC while using Jquery , AJAX for calling api from View(html page)?

Cyril Cereaic Joseph

I am performning CRUD opeartions in .net core mvc project by using an api , and List's to store all data instead of a database.I want to call the api directly from my html page using jquery and ajax.But now iam stuck with Edit operation(which is a POST method).In the edit page when iam clicking on the Save button its just refreshing the page and nothing happens.

This is the jquery,Ajax gievn in the view page for editing

@section Scripts{
    <script>
        var id = localStorage.getItem("empid");
            $.ajax({
            contentType: "application/json",
            type: "GET",
            url: "https://localhost:44347/api/Values/Edit/" + id,
            success: function (data) {

                $('#id').val(data.id)
                $('#fname').val(data.fname)
                $('#lname').val(data.lname)
                $('#location').val(data.location)
                $('#contact').val(data.contact)
                $('#email').val(data.email)
                $('#password').val(data.password)
                $('#roles').val(data.roles)
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("alertt");
                $("#postResult").val(jqXHR.statusText);
            }

        });

       $('#save').click(function () {
             var obj = {};

            obj.id = $('#id').val();
            obj.fname = $('#fname').val();
            obj.lname = $('#lname').val();
            obj.location = $('#location').val();
            obj.contact = $('#contact').val();
            obj.email = $('#email').val();
            obj.password = $('#password').val();
            obj.roles = $('#roles').val();
        $.ajax({

            type: "POST",
            url: "https://localhost:44347/api/Values/Save",
              contentType: "application/json; charset=utf-8",

                data: JSON.stringify(obj),
            success: function (data) {

              window.location.href = "/Home/Details/";
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("Error occured");
                $("#postResult").val(jqXHR.statusText);
            }

             });
        });
    </script>
}

This is the api function that is to be invoked while clicking the save button in edit page

[HttpPost("Save")]
        public IActionResult Save(StudentModel student)
        {
         obj1 = new StudentModel();
         obj1 = obj2.Save(student);
            if (obj1 != null)
            {
                return Ok(obj1);
            }
            return null;
        }
Ananthakrishna

Use this and see it if it works.

<h1>Register</h1>
<!DOCTYPE html>
<html>
<head>
</head>
<body>

  <form id="form">
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="inputfname">id</label>
        <input type="text" class="form-control" name="id" id="id" readonly>
      </div>
      <div class="form-group col-md-6">
        <label for="inputlname">fname</label>
        <input type="text" class="form-control" name="fname" id="fname">
      </div>
    </div>
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="inputfname">lname</label>
        <input type="text" class="form-control" name="lname" id="lname">
      </div>
      <div class="form-group col-md-6">
        <label for="inputlname">Location</label>
        <input type="text" class="form-control" name="location" id="location">
      </div>
    </div>
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="inputemail">contact</label>
        <input type="text" class="form-control" name="contact" id="contact">
      </div>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label for="inputcontact">Email</label>
          <input type="email" class="form-control" name="email" id="email">
        </div>
        <div class="form-group col-md-6">
          <label for="inputemail">password</label>
          <input type="password" class="form-control" name="password" id="password">
        </div>
      </div>
      <div class="form-group col-md-4">
        <label for="inputrole">Role</label>
        <input type="text" class="form-control" name="roles" id="roles" readonly>
      </div>



    </div>
    <div class="form-group">
      <div class="form-check">



      </div>
    </div>
    <button type="button" class="btn btn-primary" id="save">Save</button>
  </form>
</body>

</html>
@section Scripts{
<script>
  var id = localStorage.getItem("empid");
  $.ajax({
    contentType: "application/json",
    type: "GET",
    url: "https://localhost:44347/api/Values/Edit/" + id,
    success: function(data) {

      $('#id').val(data.id)
      $('#fname').val(data.fname)
      $('#lname').val(data.lname)
      $('#location').val(data.location)
      $('#contact').val(data.contact)
      $('#email').val(data.email)
      $('#password').val(data.password)
      $('#roles').val(data.roles)
    },
    error: function(jqXHR, textStatus, errorThrown) {
      alert("alertt");
      $("#postResult").val(jqXHR.statusText);
    }
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

POST method calling from service Angular 6 to the REST using web api in asp.net mvc

From Dev

How to call api from view using jQuery and ajax in an ASP.NET Core MVC project?

From Dev

Calling ActionResult using jquery ajax in .net core

From Dev

Calling C# method from JQuery Ajax in .NET Core Razor Pages

From Dev

Getting always an error while using Jquery Ajax Post Method in C# MVC

From Dev

jQuery Ajax GET calling wrong method in Web API Core controller

From Dev

Ajax POST method not working asp.net core

From Dev

Bad Request error when calling from jquery ajax to API contrroller ASP.net Core

From Dev

.Net Core API method not calling

From Dev

JQuery.post() method in ASP.NET Core MVC

From Dev

How to post the viewmodel to action method using JQUERY AJAX in MVC

From Dev

Download file from dotnet Core Web API using POST in ASP.NET MVC 5 controller

From Dev

Bind an object to asp mvc controller from ajax request using asp.net core and Jquery

From Dev

JQuery Ajax POST method not working? $.post

From Dev

Error 404 when calling POST method in asp.net mvc Core 2.1

From Dev

ASP.NET Core MVC calling an API

From Dev

405 Method not allowed -while calling wcf service using jquery ajax method

From Dev

Ajax Post in Asp.net Core in .Net 6 is not working in Action method

From Dev

Ajax Post, jQuery not working on the ResponseData result page

From Dev

jQuery's $.post method AJAX is not working

From Dev

Using $.ajax to POST XML to MVC Controller method

From Dev

calling servlet from php page using ajax

From Dev

how to parse json data from a rest api using C# in the ASP.Net Core MVC model and display it on a HTML page

From Dev

jQuery AJAX POST not passing value to controller in ASP.Net Core MVC

From Dev

Python - pyrfc 1.9.5 - importerror while calling from IIS hosted .NET Core api

From Dev

in Asp.net MVC Project calling an action through ajax not working

From Dev

how to post simple ID (jquery ajax) to Action method in asp.net mvc

From Dev

Getting 404 not found when calling Asp.Net Core MVC action from Jquery

From Dev

Action method won't redirect via jQuery.post in .net-core-mvc app

Related Related

  1. 1

    POST method calling from service Angular 6 to the REST using web api in asp.net mvc

  2. 2

    How to call api from view using jQuery and ajax in an ASP.NET Core MVC project?

  3. 3

    Calling ActionResult using jquery ajax in .net core

  4. 4

    Calling C# method from JQuery Ajax in .NET Core Razor Pages

  5. 5

    Getting always an error while using Jquery Ajax Post Method in C# MVC

  6. 6

    jQuery Ajax GET calling wrong method in Web API Core controller

  7. 7

    Ajax POST method not working asp.net core

  8. 8

    Bad Request error when calling from jquery ajax to API contrroller ASP.net Core

  9. 9

    .Net Core API method not calling

  10. 10

    JQuery.post() method in ASP.NET Core MVC

  11. 11

    How to post the viewmodel to action method using JQUERY AJAX in MVC

  12. 12

    Download file from dotnet Core Web API using POST in ASP.NET MVC 5 controller

  13. 13

    Bind an object to asp mvc controller from ajax request using asp.net core and Jquery

  14. 14

    JQuery Ajax POST method not working? $.post

  15. 15

    Error 404 when calling POST method in asp.net mvc Core 2.1

  16. 16

    ASP.NET Core MVC calling an API

  17. 17

    405 Method not allowed -while calling wcf service using jquery ajax method

  18. 18

    Ajax Post in Asp.net Core in .Net 6 is not working in Action method

  19. 19

    Ajax Post, jQuery not working on the ResponseData result page

  20. 20

    jQuery's $.post method AJAX is not working

  21. 21

    Using $.ajax to POST XML to MVC Controller method

  22. 22

    calling servlet from php page using ajax

  23. 23

    how to parse json data from a rest api using C# in the ASP.Net Core MVC model and display it on a HTML page

  24. 24

    jQuery AJAX POST not passing value to controller in ASP.Net Core MVC

  25. 25

    Python - pyrfc 1.9.5 - importerror while calling from IIS hosted .NET Core api

  26. 26

    in Asp.net MVC Project calling an action through ajax not working

  27. 27

    how to post simple ID (jquery ajax) to Action method in asp.net mvc

  28. 28

    Getting 404 not found when calling Asp.Net Core MVC action from Jquery

  29. 29

    Action method won't redirect via jQuery.post in .net-core-mvc app

HotTag

Archive