title: How to call API from view using jQuery and AJAX in an ASP.NET Core MVC project?
body: <p>I have created an ASP.NET MVC project to perform CRUD operations from list instead of using database, using <code>HomeController</code> 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.</p>
  <p>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.</p>
  <p>What all further details should I specify with the question?</p>
  <pre><code>&lt;script&gt;
              $(document).ready(function () {
                      $('#login').click(function () {
                       var x = $('#email1').val()
                       var y = $('#pswd').val()
   
                       $.ajax({
                           type: &quot;GET&quot;,
                           url: &quot;https://localhost:44347/api/Values/Login/&quot; + x+y,
                           contentType: &quot;application/json; charset=utf-8&quot;,
                           dataType: &quot;json&quot;,
                           success: function (data) {
                               window.location.replace(&quot;Studentdataproject\Studentdataproject\Views\Home\Details.cshtml&quot;);
                               },
                           error: function (data) {
                               alert(data.responseText);
                           }
                   });
                       });   }
  </code></pre>
  <p>This is the script that I had in login view to call login in API controller. It is not calling the API controller.</p>
  <p>API for login:</p>
  <pre><code>[HttpGet(&quot;Login/{email}/{password}&quot;)]
          public async System.Threading.Tasks.Task&lt;IActionResult&gt; LoginAsync(string email, string password)
          {
              obj1 = obj2.Login(email, password);
              if (obj1 == null)
              {
                  return Ok(null);
              }
              else
              {
                  var claims = new List&lt;Claim&gt;()
                  {
                      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();
              }
          }
  </code></pre>