Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 89245f3

Browse files
committed
added RequestHeaders to XHR
1 parent 0597034 commit 89245f3

File tree

2 files changed

+25
-70
lines changed

2 files changed

+25
-70
lines changed

example/temp.html

Lines changed: 22 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,30 @@
77
<body ng:init="$window.$root = this">
88

99
<script>
10-
function TicTacToeCntl(){
11-
this.cellStyle= {
12-
'height': '20px',
13-
'width': '20px',
14-
'border': '1px solid black',
15-
'text-align': 'center',
16-
'vertical-align': 'middle',
17-
'cursor': 'pointer'
18-
};
19-
this.reset();
20-
this.$watch('$location.hashPath', this.readUrl);
21-
}
22-
TicTacToeCntl.prototype = {
23-
dropPiece: function(row, col) {
24-
if (!this.winner && !this.board[row][col]) {
25-
this.board[row][col] = this.nextMove;
26-
this.nextMove = this.nextMove == 'X' ? 'O' : 'X';
27-
this.grade();
28-
this.setUrl();
29-
}
30-
},
31-
reset: function(){
32-
this.board = [
33-
['', '', ''],
34-
['', '', ''],
35-
['', '', '']
36-
];
37-
this.nextMove = 'X';
38-
this.winner = '';
39-
this.setUrl();
40-
},
41-
grade: function(){
42-
var b = this.board;
43-
this.winner =
44-
row(0) || row(1) || row(2) ||
45-
col(0) || col(1) || col(2) ||
46-
diagonal(-1) || diagonal(1);
47-
function row(r) { return same(b[r][0], b[r][1], b[r][2]);}
48-
function col(c) { return same(b[0][c], b[1][c], b[2][c]);}
49-
function diagonal(i) { return same(b[0][1-i], b[1][1], b[2][1+i]);}
50-
function same(a, b, c) { return (a==b && b==c) ? a : '';};
51-
},
52-
setUrl: function(){
53-
var rows = [];
54-
angular.foreach(this.board, function(row){
55-
rows.push(row.join(','));
10+
angular.widget('my:greeter', function(compileElement){
11+
var compiler = this;
12+
compileElement.css('style', 'block');
13+
var salutaitonExp = compileElement.attr('salutation');
14+
var nameExp = compileElement.attr('name');
15+
return function(linkElement){
16+
var salutaitonSpan = angular.element('<span class="salutation"></span');
17+
var nameSpan = angular.element('<span class="name"></span>');
18+
linkElement.append(salutaitonSpan);
19+
linkElement.append(compiler.text(' '));
20+
linkElement.append(nameSpan);
21+
linkElement.append(compiler.text('!'));
22+
this.$watch(salutaitonExp, function(value){
23+
salutaitonSpan.text(value);
24+
});
25+
this.$watch(nameExp, function(value){
26+
nameSpan.text(value);
5627
});
57-
this.$location.hashPath = rows.join(';') + '/' + this.nextMove;
58-
},
59-
readUrl: function(value) {
60-
if (value) {
61-
value = value.split('/');
62-
this.nextMove = value[1];
63-
angular.foreach(value[0].split(';'), function(row, i){
64-
this.board[i] = row.split(',');
65-
}, this);
66-
} else {
67-
this.reset();
68-
}
69-
}
70-
};
28+
};
29+
});
7130
</script>
72-
<h3>Tic-Tac-Toe</h3>
73-
Next Player: {{nextMove}}
74-
<div ng:show="winner">Player {{winner}} has won!</div>
75-
<table ng:controller="TicTacToeCntl">
76-
<tr ng:repeat="row in board" style="height:15px;">
77-
<td ng:repeat="cell in row" ng:style="cellStyle"
78-
ng:click="dropPiece($parent.$index, $index)">{{cell}}</td>
79-
</tr>
80-
</table>
81-
<button ng:click="reset()">reset board</button>
31+
<div ng:init="salutation='Hello'; name='World'">
32+
<my:greeter salutation="salutation" name="name"/>
33+
</div>
8234

8335
</body>
8436
</html>

src/Browser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ Browser.prototype = {
7474
var xhr = new this.XHR(),
7575
self = this;
7676
xhr.open(method, url, true);
77+
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
78+
xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
79+
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
7780
this.outstandingRequests.count ++;
7881
xhr.onreadystatechange = function() {
7982
if (xhr.readyState == 4) {

0 commit comments

Comments
 (0)