Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Microsoft.AspNetCore.Mvc;
public class ApiController : Controller
{
[HttpGet]
public IActionResult GetMessage()
{
return Json(new { message = "Hello from server" });
}
[HttpPost]
public IActionResult PostData(string name)
{
return Json(new { message = "Received " + name });
}
}
/* Razor View (Index.cshtml) */
@*
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
// GET AJAX
$.get("/api/getmessage", function (data) {
console.log(data.message);
});
// POST AJAX
$.post("/api/postdata", { name: "Alice" }, function (data) {
console.log(data.message);
});
</script>
*@Coding works best on desktop or with an external keyboard.