call controller method from view using jquery ajax

Solutions on MaxInterview for call controller method from view using jquery ajax by the best coders in the world

showing results for - "call controller method from view using jquery ajax"
Salvador
27 Apr 2018
1// Credits to: https://www.aspsnippets.com/Articles/ASPNet-MVC-Call-Controller-Method-from-View-using-jQuery-AJAX.aspx
2
3$.ajax({
4    type: "POST",
5    url: "/Home/AjaxMethod",
6    data: '{name: "' + $("#txtName").val() + '" }',
7    contentType: "application/json; charset=utf-8",
8    dataType: "json",
9    success: function (response) {
10        alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);
11    },
12    failure: function (response) {
13        alert(response.responseText);
14    },
15    error: function (response) {
16        alert(response.responseText);
17    }
18});