0

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

var baseurl = 'http://localhost:58782/api/Appointments/UserAppointments/[email protected]/'

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

4
  • And what error does $.ajax report? Commented May 26, 2018 at 9:15
  • in console it is showing text error. no error detail comes. Commented May 26, 2018 at 9: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`? Commented May 26, 2018 at 9: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); }` Commented May 26, 2018 at 9:20

1 Answer 1

0

Use %40 for @. As in

var baseurl = 'http://localhost:58782/api/Appointments/UserAppointments/[email protected]/'

should be

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

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

Sign up to request clarification or add additional context in comments.

13 Comments

i saw my web api action is getting called fine with this base url var baseurl = 'localhost:58782/api/Appointments/UserAppointments/…' and return data but ajax is throwing error
i capture the error text that is No HTTP resource was found that matches the request URI '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); }`
how to convert [email protected] to tridip%40gmail.com programmatically by javascript
encodeURIComponent is not working var baseurl = '@ConfigurationManager.AppSettings["baseAddress"]' + 'api/Appointments/UserAppointments/' + encodeURIComponent(useremail) + '/';
using encodeURIComponent i am getting this url localhost:58782/api/Appointments/UserAppointments/ where email is missing and url end with two //
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.