I have created an ASP.NET MVC project to perform CRUD operations from list instead of using database, using HomeController and an API controller. Now I want to edit that project such a way that I want to call API functions directly from my view which is an HTML page.
The control is passing from view to home controller then to API controller. I want a direct connection between view and API using jQuery and AJAX.
What all further details should I specify with the question?
<script>
$(document).ready(function () {
$('#login').click(function () {
var x = $('#email1').val()
var y = $('#pswd').val()
$.ajax({
type: "GET",
url: "https://localhost:44347/api/Values/Login/" + x+y,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
window.location.replace("Studentdataproject\Studentdataproject\Views\Home\Details.cshtml");
},
error: function (data) {
alert(data.responseText);
}
});
}); }
This is the script that I had in login view to call login in API controller. It is not calling the API controller.
API for login:
[HttpGet("Login/{email}/{password}")]
public async System.Threading.Tasks.Task<IActionResult> LoginAsync(string email, string password)
{
obj1 = obj2.Login(email, password);
if (obj1 == null)
{
return Ok(null);
}
else
{
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Name,obj1.email),
new Claim(ClaimTypes.Role,obj1.roles)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var princilple = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, princilple);
return Ok();
}
}
