我创建了一个 ASP.NET MVC 项目来从列表执行 CRUD 操作,而不是使用数据库、使用HomeControllerAPI 控制器。现在我想编辑该项目,以便直接从我的视图(即 HTML 页面)调用 API 函数。
控制从视图传递到家庭控制器,然后传递到 API 控制器。我希望使用 jQuery 和 AJAX 在视图和 API 之间建立直接连接。
我应该在问题中详细说明哪些详细信息?
<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);
}
});
}); }
Run Code Online (Sandbox Code Playgroud)
这是我在登录视图中调用 API 控制器登录的脚本。它没有调用 API 控制器。
登录API:
[HttpGet("Login/{email}/{password}")]
public async System.Threading.Tasks.Task<IActionResult> LoginAsync(string email, string password)
{ …Run Code Online (Sandbox Code Playgroud) asp.net-mvc jquery asp.net-ajax asp.net-core-mvc asp.net-core-webapi