Ошибка Jjery Ajax при вызове действия веб-API - PullRequest
0 голосов
/ 26 мая 2018

Это мой код jquery ajax, который я использую для вызова действия веб-API.

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 () {


});

Это мой код веб-API, который возвращает IEnumerable<Entities.UserAppointments> на System.Net.Http.HttpResponseMessage

Полныйкод веб-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; }
}

Моя цель - захватить IEnumerable<Entities.UserAppointments> с помощью jquery, выполнить итерацию в данных и динамически добавить эти данные в таблицу.

Пожалуйста, укажите мнекак это сделать.спасибо

1 Ответ

0 голосов
/ 26 мая 2018

Используйте% 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...