[英]An extremely simple AJAX "HTTP GET" request using jQuery 's $.ajax() method does not Work for me. Backend is a ASP.net WEB API controller
Im trying to learn basics of WEB API, Im very beginner.我正在尝试学习 WEB API 的基础知识,我非常初学者。 I connected a HTML webpage to a get method in a WEB API controller in c#, using AJAX request with XMLHttpRequest object successfully.我将 HTML 网页连接到 WEB API controller c# 中的 get 方法,成功使用 AJAX 请求和 XMLHttpRequest 88408299 But Im trying to figure out how to write it with jQuery $.ajax method and I just cant figure it out.但我试图弄清楚如何用 jQuery $.ajax 方法编写它,但我就是想不通。
The project is solely for me figuring out how to write these requests just for fun:D, thats why it is written as trivially as possible.该项目完全是为了让我弄清楚如何编写这些请求只是为了好玩:D,这就是为什么它写得尽可能简单。
What I see when I launch the HTML site is only number 7 - response from xmlhttprequest object, so the connection to the controller must work.当我启动 HTML 站点时,我看到的只是数字 7 - 来自 xmlhttprequest object 的响应,因此与 controller 的连接必须有效。
This is the HTML website code:这是 HTML 网站代码:
<html>
<head>
<script type="text/javascript" src="jquery-3.5.1.min.js">
</script>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
let xml = new XMLHttpRequest();
xml.onload = () => {
let d = JSON.parse(xml.response);
document.write(d.Id);
}
xml.open("GET", "https://localhost:44335/api/employees");
xml.send();
$.ajax({
type: "GET",
dataType: "json",
url: "https://localhost:44335/api/employees2",
success: function (data) {
document.write("gh");
},
error: function (data) {
alert("chyba");
}
});
</script>
</body>
</html>
This is the backend C# WEB API Controller code这是后端 C# WEB API Controller 代码
namespace WebApplication8.Controllers
{
class employee
{
public string Id { get; set; }
}
public class EmployeesController : ApiController
{
[Route("api/employees")]
[HttpGet]
public HttpResponseMessage Geti()
{
employee e = new employee();
e.Id = "7";
return Request.CreateResponse(HttpStatusCode.OK,e);
}
[Route("api/employees2")]
[HttpGet]
public HttpResponseMessage a()
{
employee v = new employee();
v.Id = "8";
return Request.CreateResponse(HttpStatusCode.OK, v);
}
}
}
you have to fix ajax on success你必须成功修复 ajax
$.ajax({
type: "GET",
dataType: "json",
url: "https://localhost:44335/api/employees2",
success: function (data) {
document.write(data.Id);
},
error: function (xhr) {
alert(xhr.message);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.