te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How do you return status 401 from WebAPI to AngularJS and also include a custom message? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you return status 401 from WebAPI to AngularJS and also include a custom message? - Stack Overflow

programmeradmin4浏览0评论

In my WebAPI class, an ApiController, I make a call such as:

string myCustomMessage = .....;

throw new HttpResponseException(
    new HttpResponseMessage(HttpStatusCode.Unauthorized)
        { ReasonPhrase = myCustomMessage });

When I call using AngularJS $resource service, I do get 401 in the status field the response, in the catch block of the promise. The 401 matches HttpStatusCode.Unauthorized, so all's well.

The problem, however, is that the data field of the response is empty (null). I don't get the myCustomMessage returned.

Now, if rather than throwing a HttpResponseException exception, I just throw a regular Exception with a message, that message does make it sway back to Angular.

I need to be able to do both: return a custom message from the server, as well as have the returned status code be whatever I want, in this case 401.

Anyone know how to make that work?

[edit] Solution:

 throw new HttpResponseException(
     Request.CreateErrorResponse(HttpStatusCode.Unauthorized, myCustomMessage));

In my WebAPI class, an ApiController, I make a call such as:

string myCustomMessage = .....;

throw new HttpResponseException(
    new HttpResponseMessage(HttpStatusCode.Unauthorized)
        { ReasonPhrase = myCustomMessage });

When I call using AngularJS $resource service, I do get 401 in the status field the response, in the catch block of the promise. The 401 matches HttpStatusCode.Unauthorized, so all's well.

The problem, however, is that the data field of the response is empty (null). I don't get the myCustomMessage returned.

Now, if rather than throwing a HttpResponseException exception, I just throw a regular Exception with a message, that message does make it sway back to Angular.

I need to be able to do both: return a custom message from the server, as well as have the returned status code be whatever I want, in this case 401.

Anyone know how to make that work?

[edit] Solution:

 throw new HttpResponseException(
     Request.CreateErrorResponse(HttpStatusCode.Unauthorized, myCustomMessage));
Share Improve this question edited Apr 13, 2014 at 5:58 Daisha Lynn asked Apr 12, 2014 at 3:48 Daisha LynnDaisha Lynn 4591 gold badge4 silver badges16 bronze badges 2
  • What happens if you use plain $http? – Karolis Juodelė Commented Apr 12, 2014 at 4:01
  • Karolis, I am okay with using plain $http. I would do that if we've determined that $resource cannot be used to solve this problem. Has that been confirmed? – Daisha Lynn Commented Apr 13, 2014 at 4:45
Add a ment  | 

2 Answers 2

Reset to default 11

Try returning HttpResponseMessage like this.

public HttpResponseMessage Get()
{
    return Request.CreateErrorResponse(
              HttpStatusCode.Unauthorized, "You are not authorized");
}

This should produce an HTTP response message like this.

HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
Date: Sat, 12 Apr 2014 07:12:54 GMT
Content-Length: 36

{"Message":"You are not authorized"}

I use a response interceptor for that. A response interceptor is pretty much a "filter" where all responses pass through and I can ask about there status code and perform logic.

Something like this:

myModule.factory('responseInterceptor', function ($q) {
    return {
        response: function (res) {
           switch(res.status){
             case 401:
                     // do what you want
           }
           return res;
        },
        responseError: function (res) {
            return $q.reject(res);
        }
    };
});

myModule.config(function ($httpProvider) {
    $httpProvider.interceptors.push('responseInterceptor');
});

About your custom message, you can add it in a http header. In my case, when I get a 401, I add a location header with a url and redirect to it.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论