【问题标题】:Jquery Ajax throw error when calling web api action调用 Web api 操作时 Jquery Ajax 抛出错误
【发布时间】: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&lt;Entities.UserAppointments&gt; 并迭代数据并将这些数据动态附加到表中。

请指导我如何做到这一点。谢谢

【问题讨论】:

  • $.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


【解决方案1】:

使用 %40 表示 @。 如

var baseurl = 'http://localhost:58782/api/Appointments/UserAppointments/tridip@gmail.com/'

应该是

var baseurl = 'http://localhost:58782/api/Appointments/UserAppointments/tridip%40gmail.com/'

见:https://www.w3schools.com/jsref/jsref_encodeURI.asp

【讨论】:

  • 我看到我的 web api 操作被这个基本 url var baseurl = 'localhost:58782/api/Appointments/UserAppointments/…' 很好地调用并返回数据但 ajax 抛出错误
  • 我捕获了错误文本,即未找到与请求 URI 'localhost:58782/api/Appointments/UserAppointments' 匹配的 HTTP 资源。这样我捕获`错误:函数(xhr,textStatus,errorThrown){var err = eval(“(”+ xhr.responseText +“)”);警报(错误消息);控制台.log(文本状态); }`
  • 如何通过 javascript 以编程方式将 tridip@gmail.com 转换为 tridip%40gmail.com
  • encodeURIComponent 不工作var baseurl = '@ConfigurationManager.AppSettings["baseAddress"]' + 'api/Appointments/UserAppointments/' + encodeURIComponent(useremail) + '/';
  • 使用 encodeURIComponent 我得到这个 url localhost:58782/api/Appointments/UserAppointments/,其中缺少电子邮件并且 url 以两个 // 结尾
猜你喜欢
  • 2013-02-06
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 2013-10-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多