Angular2 http获取response header对象

Angular2 http获取response header对象,第1张

很多时候需要获取响应头来进行一些 *** 作,比如获取响应头中的Date来缓存请求时间,获取自定义的token进行用户登录验证等等。

如何拿到上面的信息呢,这里以获取Date值举例,相信很多人发现,在成功回调中打印header对象中并没有Date属性,如下:

为什么header对象中只有Content-Type属性呢,因为如果你与服务端同域,你可以获得所有header对象属性,但是如果不同域的话,需要在服务端设置 expose_headers 。

Before

After

这样设置完后 就能通过resheaderget('Date')获取响应头中Date的值了。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

<!DOCTYPE html>

<html ng-app="test">

<head>

<title>Freetrial</title>

</head>

<body ng-controller="FreetrialController">

<div class="xsy" ng-repeat="item in freetrial" ng-if="use('xy0001', 0, item)" id="xy0001">

<a href="{{itemsdcx()}}"><img src="">{{itemimgurl}}</a>

<h1>{{itemgoodstitle}} </h1>

</div>

<script type="text/javascript" src="/lib/angularminjs"></script>

<script>

var app = angularmodule('test', []);

appcontroller('FreetrialController', function($scope){

$scopeuse = function(id, status, item){

var flag = false;

if(!item || (id === itemgoodsid && status === itemstatus)){

flag = true;

}

return flag;

}

$scopefreetrial = [

{

goodsid:"xy0001",

imgurl:"img/178jpg",

goodstitle:"好娃娃牌安全椅",

quantity:"10份",

cost:896,

linkt:"sdcx()",

status:0

},

{

goodsid:"xy0002",

imgurl:"img/178jpg",

goodstitle:"时尚包",

quantity:"10份",

cost:298,

status:1

},

{

goodsid:"xy0003",

imgurl:"img/178jpg",

goodstitle:"好娃娃",

quantity:"10份",

cost:896,

linkt:"sdcx()",

status:0

}

]

});

</script>

</body>

</html>

<!--还可以从数据上着手, 先把数据按照需求重组,再输出到视图-->

字符串转化成json数据,json数据的遍历

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>作用域</title>

<script src="js/angularminjs"></script>

<style type="text/css">

</style>

</head>

<body ng-app="my-frist-app">

<div ng-controller="fristController" >

</div>

<script type="text/javascript">

var myApp = angularmodule('my-frist-app',[])

myAppcontroller('fristController',function($scope,$rootScope){

// 对象才用copy,基本数据类型不用。

var obj3 = {name:'zhangsan'};

var obj4 = {name:'lisi'};

angularcopy(obj3,obj4);

consolelog(obj3);

consolelog(obj4);

// 继承 obj5继承了obj6(所有的属性和方法) copy是前者给了后者。

var obj5 = {name:'zhangsan'};

var obj6 = {age:'40'};

angularextend(obj5,obj6);

consolelog(obj5);

consolelog(obj6);

// 可以遍历数组和对象。 遍历 1 遍历的对象 2 遍历的方法 3遍历的上下文环境(一般不用)

var json = {name:"zhangsan",age:40};

angularforEach(json,function( val,key){

consolelog(key);

consolelog(val)

});

// 绑定对象

var obj7 = {name:'lisi'};

var fun = angularbind(obj7,function(){

consolelog(thisname);

})

fun();

// 字符串和对象的转化

//JSONparse(); js 方法

var jsonStr = '{"name":"李四"}';

consolelog(angularfromJson(jsonStr));

// 对象转字符串

//JSONstringify() js方法

consolelog(angulartoJson(obj7));

});

</script>

</body>

</html>

以上就是关于Angular2 http获取response header对象全部的内容,包括:Angular2 http获取response header对象、angularjs怎么拿到 数组中的一个值、angularjs对象转换成字符串后怎么取值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10088398.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存