有没有办法使用 JS 和 AngularJS 获取 HTTP 状态代码名称? [英] Is there a way to get HTTP status code name using JS and AngularJS?

查看:31
本文介绍了有没有办法使用 JS 和 AngularJS 获取 HTTP 状态代码名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AngularJS、JS、JQ、HTML5、CSS3 网络应用程序.将不同的 HTTP 方法发送到我们项目的 REST API 并对其进行操作.它与 Restlet(以前的 Dev HTTP Client)的 DHC 具有类似的行为.每个请求都会返回一个状态代码,如 200、201、404、500 等,然后显示给用户.

I have an AngularJS, JS, JQ, HTML5, CSS3 web app. It is suposed to send different HTTP methods to REST API of our projects and manipulate them. It has similar behavior to DHC by Restlet (formerly Dev HTTP Client). Every request returns a status code like 200, 201, 404, 500 etc. which is then displayed to the user.

现在我想要的不仅是显示响应代码,还要显示它的描述,如下所示:

Now what I would like is to display not only a response code, but also a description of it like this:

404 Not Found201 Created

我从 Angular 收到这样的回复:

I am getting a response from Angular like this:

$http(config).success(function (data, status, headers, config) {//some logic}

有谁知道这是否可以使用 AngularJS 实现?

Does anyone know if this is possible using AngularJS?

推荐答案

如果我没记错的话,你仍然可以使用 jQuery ajax 进行调用,并使用 $.ajaxSetup 设置你的响应,如下所示:

If I'm not wrong, you can still use jQuery ajax for your call, and setup your response with $.ajaxSetup like this:

$.ajaxSetup({
    type: "GET",
    dataType: "jsonp",
    error: function(xhr, exception){
        if( xhr.status === 0)
            alert('Error : ' + xhr.status + 'You are not connected.');
        else if( xhr.status == "201")
            alert('Error : ' + xhr.status + '\nServer error.');
        else if( xhr.status == "404")
            alert('Error : ' + xhr.status + '\nPage note found');
        else if( xhr.status == "500")
             alert('Internal Server Error [500].');
        else if (exception === 'parsererror') 
            alert('Error : ' + xhr.status + '\nImpossible to parse result.');
        else if (exception === 'timeout')
            alert('Error : ' + xhr.status + '\nRequest timeout.');
        else
            alert('Error .\n' + xhr.responseText);
    }
});

这篇关于有没有办法使用 JS 和 AngularJS 获取 HTTP 状态代码名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆