【发布时间】:2018-05-26 17:22:32
【问题描述】:
这是我用来调用 web api 操作的 jquery ajax 代码。
var baseurl = 'http://localhost:58782/api/Appointments/UserAppointments/tridip@gmail.com/'
$.ajax({
url: baseurl,
type: 'GET',
dataType: 'json',
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log(textStatus);
}
}).done(function () {
});
这是我的 web api 代码,它通过 System.Net.Http.HttpResponseMessage 返回 IEnumerable<Entities.UserAppointments>
完整的网络 API 代码。它已经过测试并且可以正常工作。我猜错误是在 jquery ajax 代码中。
[System.Web.Http.HttpGet, System.Web.Http.Route("UserAppointments/{email}")]
public System.Net.Http.HttpResponseMessage UserAppointments(string email = null)
{
System.Net.Http.HttpResponseMessage retObject = null;
if (!string.IsNullOrEmpty(email))
{
UserAppointmentService _appservice = new UserAppointmentService();
IEnumerable<Entities.UserAppointments> app = _appservice.GetAllUserAppointments(email);
if (app.Count() <= 0)
{
var message = string.Format("No appointment found for the user [{0}]", email);
HttpError err = new HttpError(message);
retObject = Request.CreateErrorResponse(System.Net.HttpStatusCode.NotFound, err);
retObject.ReasonPhrase = message;
}
else
{
retObject = Request.CreateResponse(System.Net.HttpStatusCode.OK, app);
}
}
else
{
var message = string.Format("No email provided");
HttpError err = new HttpError(message);
retObject = Request.CreateErrorResponse(System.Net.HttpStatusCode.NotFound, err);
retObject.ReasonPhrase = message;
}
return retObject;
}
public class UserAppointments
{
public int ID { get; set; }
public string DoctorName { get; set; }
public string AvailableDate { get; set; }
public string AvailableTime { get; set; }
public string Email { get; set; }
}
我的目标是通过 jquery 捕获 IEnumerable<Entities.UserAppointments> 并迭代数据并将这些数据动态附加到表中。
请指导我如何做到这一点。谢谢
【问题讨论】:
-
而
$.ajax报告了什么错误? -
在控制台中显示文本错误。没有错误细节出现。
-
errorThrown持有哪些信息?并且开发者工具的 Network 选项卡也会显示相关信息,例如 http 错误代码。是`4xx`还是`5xx`? -
我捕获错误文本
No HTTP resource was found that matches the request URI 'http://localhost:58782/api/Appointments/UserAppointments/'.这样我捕获`error: function (xhr, textStatus, errorThrown) { var err = eval("(" + xhr.responseText + ")" );警报(错误消息);控制台.log(文本状态); }`
标签: jquery ajax asp.net-web-api