本文介绍了使用ASP.NET的WebAPI返回布尔通过一个Ajax GET请求被消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个的WebAPI(使用第2版),它返回的Htt presponseMessage 。我做 AJAX 要求对这些的WebAPI方法和的WebAPI返回 JSON 响应。这是所有罚款和花花公子,但现在我需要的是一种方法,使一个 AJAX GET请求来的WebAPI方法,该方法只返回一个布尔值。下面是我使用获得 GET 请求的例子 JSON

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

我希望做到的是像(这是伪code):

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

其中, hasMessageBeenDisplayed 应该是在我的WebAPI方法返回。这是我的WebAPI方法的一个例子:

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

要能够使一个 AJAX 要求,即希望,我还会回的Htt presponseMessage 从我的WebAPI的方法?我怎样才能使一个 AJAX 的GET请求的方法,它的调用可以返回

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?

推荐答案

为什么不能简单地改变为:

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();
}

这篇关于使用ASP.NET的WebAPI返回布尔通过一个Ajax GET请求被消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 01:02