5

I have built a WebAPI (using version 2) that returns HttpResponseMessage. I make AJAX requests to these WebAPI methods, and the WebAPI returns a JSON response. This is all fine and dandy, but now what I need is a way to make an AJAX GET request to a WebAPI method that simply returns a Boolean. Here's an example of a GET request I'm using to get JSON:

$.ajax({
    url: 'http://server/site/api/BulletinBoard/GetUserMessageHistory?userId=' + userId + '&messageId=' + messageId,
    type: 'GET',
    dataType: 'json',
    crossDomain: true,
    success: function (data) {
          DoSomething();                    
    },
    error: function (x, y, z) {
          alert(x + '\n' + y + '\n' + z);
    }
});

What I hope to accomplish is something like (this is pseudo-code):

    var hasMessageBeenDisplayed = 
        $.ajax({
              url: 'http://server/site/api/BulletinBoard/GetUserMessageHistory?userId=' + userId + '&messageId=' + messageId,
              type: 'GET',
              dataType: 'json',
              crossDomain: true,
              success: function (data) {
                   DoSomething();                    
              },
              error: function (x, y, z) {
                   alert(x + '\n' + y + '\n' + z);
              }
       });

Where hasMessageBeenDisplayed would be either true or false returned by my WebAPI method. Here's an example of my WebAPI method:

[HttpGet]
public HttpResponseMessage GetUserMessageHistory(string userId, int messageId)
{
    var userMessageHistory = (from i in db.UserMessageHistories
                              where i.UserId == userId &&
                              i.MessageId == messageId
                              select new
                              {
                                  UserId = i.UserId,
                                  MessageId = i.MessageId,
                                  LastSeen = i.LastSeen,
                              }).ToList();

    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, userMessageHistory);
    return response;
}

To be able to make an AJAX request that expects true or false, would I still return HttpResponseMessage from my WebAPI method? How can I make an AJAX GET request whose method it's calling can return true or false?

1
  • the $.ajax() return the XMLHttpRequest, if the webapi return 200 status code, it will execute the success event of your call. Since you set the json in the ajax call, you are sending the request to webapi with the content-type application/json and you will get an output in this format. Commented Jan 10, 2014 at 16:20

1 Answer 1

3

Why don't simply change as:

[HttpGet]
public bool GetUserMessageHistory(string userId, int messageId)
{
    var userMessageHistory = (from i in db.UserMessageHistories
                              where i.UserId == userId &&
                              i.MessageId == messageId
                              select new
                              {
                                  UserId = i.UserId,
                                  MessageId = i.MessageId,
                                  LastSeen = i.LastSeen,
                              }).ToList();


    return userMessageHistory.any();
}
Sign up to request clarification or add additional context in comments.

10 Comments

how about userMessageHistory ?
Will this work? I thought of this but didn't try it because I make an HTTP GET request to my WebAPI method, and the caller expects an HttpResponse, not a .NET Boolean return value. The dataType defined in the AJAX request can only be json, xml, and a couple others, but certainly not a boolean.
I mean, if this is true, then how would I construct my AJAX call? Obviously it wouldn't be a JSON response anymore, right?
@FelipeOriani it's just to figure out, don't be afraid about the content.
@Mike Marks I think you are misunderstanding how WebAPI works. WebAPI is smart enough to know that since you have a method in a controller that you are returning an HTTP response. So even when you return primitive types such as int, string, and bool, it will send it out in XML or JSON depending on what you have it configured for. It's that simple.
|

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.