Skip to content

Commit 6aec0ff

Browse files
committed
fixed a escaping
1 parent ea7c3d5 commit 6aec0ff

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Chapter5.markdown

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
##通过$http进行通行
2828

2929
从Ajax应用(使用XMLHttpRequests)发动一个请求到服务器的传统方式包括:得到一个XMLHttpRequest对象的引用、发起请求、读取响应、检验错误代码然后最后处理服务器响应。它就是下面这样:
30-
30+
3131
var xmlhttp = new XMLHttpRequest();
3232
xmlhttp.onreadystatechange = function() {
3333
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
@@ -40,27 +40,27 @@
4040
xmlhttp.open(“GET”, “http://myserver/api”, true);
4141
// Make the request
4242
xmlhttp.send();
43-
43+
4444
对于这样一个简单、常用且经常重复的任务,上面这个代码量比较大.如果你想重复性地做这件事,你最终可能会做一个封装或者使用现成的库.
4545

4646
AngularJS XHR(XMLHttpRequest) API遵循Promise接口.因为XHRs是异步方法调用,服务器响应将会在未来一个不定的时间返回(当然希望是越快越好).Promise接口保证了这样的响应将会如何处理,它允许Promise接口的消费者以一种可预计的方式使用这些响应.
4747

4848
假设我们想从我们的服务器取回用户的信息.如果接口在/api/user地址可用,并且接受id作为url参数,那么我们的XHR请求就可以像下面这样使用Angular的核心$http服务:
49-
49+
5050
$http.get('api/user', {params: {id: '5'}
5151
}).success(function(data, status, headers, config) {
5252
// Do something successful.
5353
}).error(function(data, status, headers, config) {
5454
// Handle the error
5555
});
56-
56+
5757
如果你来自jQuery世界,你可能会注意到:AngularJS和jQuery处理异步需求的方式很相似.
5858

5959
我们上面例子中使用的$htttp.get方法仅仅是AngularJS核心服务$http提供的众多简便方法之一.类似的,如果你想使用AngularJS向相同URL带一些POST请求数据发起一个POST请求,你可以像下面这样做:
6060

6161
var postData = {text: 'long blob of text'};
6262
// The next line gets appended to the URL as params
63-
// so it would become a post request to /api/user?id=5
63+
// so it would become a post request to /api/user?id=5
6464
var config = {params: {id: '5'}};
6565
$http.post('api/user', postData, config
6666
).success(function(data, status, headers, config) {
@@ -138,16 +138,16 @@ AngularJS有一个默认的头信息,这个头信息将会对所有的发送请
138138
// Set DO NOT TRACK for all Get requests
139139
$httpProvider.default.headers.get['DNT'] = '1';
140140
});
141-
141+
142142
如果你只想对某个特定的请求设置头信息,而不是设置默认头信息.那么你可以通过给$http服务传递包含指定头信息的config对象来做.相同的定制头信息可以作为第二个参数传递给GET请求,第一个参数是URL字符串:
143-
143+
144144
$http.get('api/user', {
145145
// Set the Authorization header. In an actual app, you would get the auth
146146
// token from a service
147147
headers: {'Authorization': 'Basic Qzsda231231'},
148148
params: {id: 5}
149149
}).success(function() { // Handle success });
150-
150+
151151
如何在应用中处理权限验证头信息的成熟示例将会在第八章的Cheetsheets示例部分给出.
152152

153153
###缓存响应数据
@@ -210,7 +210,7 @@ AngularJS对所有`$http`服务发起的请求和响应做一些基本的转换,
210210
+ 控制器能够正确地把响应数据存储到作用域scope的`names`变量属性中.
211211

212212
在我们的单元测试中构造一个控制器时,我们给它注入一个scope作用域和一个伪造的HTTP服务,在构建测试控制器的方式和生产中构建控制器的方式其实是一样的.这是推荐方法,尽管它看上去上有点复杂。让我看一下具体代码:
213-
213+
214214
describe('NamesListCtrl', function(){
215215
var scope, ctrl, mockBackend;
216216

@@ -233,7 +233,7 @@ AngularJS对所有`$http`服务发起的请求和响应做一些基本的转换,
233233
it('should fetch names from server on load', function() {
234234
// Initially, the request has not returned a response
235235
expect(scope.names).toBeUndefined();
236-
236+
237237
// Tell the fake backend to return responses to all current requests
238238
// that are in flight.
239239
mockBackend.flush();
@@ -301,7 +301,7 @@ AngularJS对所有`$http`服务发起的请求和响应做一些基本的转换,
301301
}]);
302302

303303
取代以上方式,你也可以轻松创建一个在你的应用中始终如一的Angular资源服务,就像下面代码这样:
304-
304+
305305
myAppModule.factory('CreditCard', ['$resource', function($resource) {
306306
return $resource('/user/:userId/card/:cardId',
307307
{userId: 123, cardId: '@id'},
@@ -386,12 +386,12 @@ ngResource依赖项是一个封装,它以Angular核心服务`$http`为基础.因
386386
// Assume that CreditCard resource is used by the controller
387387
ctrl = $controller(CreditCardCtrl, {$scope: scope});
388388
}));
389-
389+
390390
it('should fetched list of credit cards', function() {
391391
// Set expectation for CreditCard.query() call
392392
mockBackend.expectGET('/user/123/card').
393393
respond([{id: '234', number: '11112222'}]);
394-
394+
395395
ctrl.fetchAllCards();
396396

397397
// Initially, the request has not returned a response
@@ -438,7 +438,7 @@ ngResource依赖项是一个封装,它以Angular核心服务`$http`为基础.因
438438
另外,这种情况对错误处理也有很大影响.错误处理的最好方法是什么?在每次都做错误处理?那代码结构就会非常乱.
439439

440440
为了解决上面这些问题,预期值建议(Promise proposal)机制提供了一个then函数的概念,这个函数会在响应成功返回的时候调用相关的函数去执行,另一方面,当产生错误的时候也会干相同的事,这样整个代码就有嵌套结构变为链式结构.所以之前那个例子用预期值API机制(至少在AngularJS中已经被实现的)改造一下,代码结构会平整许多:
441-
441+
442442
var deferred = $q.defer();
443443
var fetchUser = function() {
444444
// After async calls, call deferred.resolve with the response value
@@ -483,7 +483,7 @@ ngResource依赖项是一个封装,它以Angular核心服务`$http`为基础.因
483483
});
484484
}
485485
});
486-
486+
487487
// Ensure that the interceptor we created is part of the interceptor chain
488488
$httpProvider.responseInterceptors.push('myInterceptor');
489489

@@ -530,7 +530,7 @@ AngularJS将会自动的把前缀字符串过滤掉,然后仅仅处理真实JSON
530530

531531
考虑依稀下面这个跨站请求伪造攻击的案例:
532532

533-
+ 用户A登录进他的银行帐号(http\:\/\/www.examplebank.com/)
533+
+ 用户A登录进他的银行帐号(http://www.examplebank.com/)
534534
+ 用户B意识到这点,然后诱导用户A访问用户B的个人主页
535535
+ 主页上有一个特殊手工生成的图片连接地址,这个图片的的指向地址将会导致一次跨站请求伪造攻击,比如如下代码:
536536
`<img src="http://www.examplebank.com/xfer?from=UserA&amount=10000&to=UserB" />`

0 commit comments

Comments
 (0)