0

This is my jquery ajax code which i am using to call web api action.

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


});

This is my web api code which return IEnumerable<Entities.UserAppointments> by System.Net.Http.HttpResponseMessage

Full web API code. It is tested and working. I guess the error is in jquery ajax code.

[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; }
}

My objective is to capture IEnumerable<Entities.UserAppointments> by jquery and iterate in data and append those data in table dynamically.

Please guide me how to do it. thanks

t.niese
  • 39,256
  • 9
  • 74
  • 101
Mist
  • 684
  • 9
  • 30
  • And what error does `$.ajax` report? – t.niese May 26 '18 at 09:15
  • in console it is showing text error. no error detail comes. – Mist May 26 '18 at 09:16
  • What information does `errorThrown` hold? And also the Network tab of the developer tools would show you relevant informations like http error code. Is it a ` 4xx` or a ` 5xx`? – t.niese May 26 '18 at 09:19
  • i capture the error text that is `No HTTP resource was found that matches the request URI 'http://localhost:58782/api/Appointments/UserAppointments/'.` this way i capture ` error: function (xhr, textStatus, errorThrown) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); console.log(textStatus); }` – Mist May 26 '18 at 09:20

1 Answers1

0

Use %40 for @. As in

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

should be

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

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

HelloWorld
  • 3,381
  • 5
  • 32
  • 58
  • i saw my web api action is getting called fine with this base url var baseurl = 'http://localhost:58782/api/Appointments/UserAppointments/tridip@gmail.com/' and return data but ajax is throwing error – Mist May 26 '18 at 09:23
  • i capture the error text that is No HTTP resource was found that matches the request URI 'http://localhost:58782/api/Appointments/UserAppointments/'. this way i capture ` error: function (xhr, textStatus, errorThrown) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); console.log(textStatus); }` – Mist May 26 '18 at 09:24
  • how to convert tridip@gmail.com to tridip%40gmail.com programmatically by javascript – Mist May 26 '18 at 09:27
  • encodeURIComponent is not working `var baseurl = '@ConfigurationManager.AppSettings["baseAddress"]' + 'api/Appointments/UserAppointments/' + encodeURIComponent(useremail) + '/'; ` – Mist May 26 '18 at 09:30
  • using encodeURIComponent i am getting this url http://localhost:58782/api/Appointments/UserAppointments// where email is missing and url end with two // – Mist May 26 '18 at 09:31
  • What are Razor variables and what are JavaScript variables in that statement? Paste the full Razor code – HelloWorld May 26 '18 at 09:32
  • Better yet, paste the JavaScript that results from the Razor code – HelloWorld May 26 '18 at 09:33
  • $(document).ready(function () { var useremail = '@User.Identity.Name'; var baseurl = '@ConfigurationManager.AppSettings["baseAddress"]' + 'api/Appointments/UserAppointments/' + useremail + '/'; }); useremail has value called tridip@gmail.com – Mist May 26 '18 at 09:38
  • That still looks like a mixture of Razor and JS :) what is the resulting JS? – HelloWorld May 26 '18 at 09:45
  • this issue sorted with encodeURIComponent . thanks a lot. – Mist May 26 '18 at 09:51
  • can you tell me how could i iterate in json which return by web api and build table rows. – Mist May 26 '18 at 09:51
  • Don't understand. think you should ask another question if it is totally different. – HelloWorld May 26 '18 at 10:58
  • yes asked. here is the url https://stackoverflow.com/questions/50541967/jquery-ajax-how-to-iterate-in-json-and-build-html-table – Mist May 26 '18 at 11:03