Skip to content

Commit 965a2f6

Browse files
author
zhuufengpeixun
committedJul 7, 2015
image
1 parent 060c2a7 commit 965a2f6

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed
 

‎.idea/workspace.xml

+29-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎htmlserver.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ http.createServer(function(req,res){
1010
});
1111
} else if(url == '/addUser'){
1212
req.on('data',function(chunk){
13-
console.log(chunk);
14-
users.push(chunk);
13+
users.push(change(chunk.toString()));
1514
});
1615
req.on('end',function(){
17-
res.end(users);
16+
res.end(JSON.stringify(users));
1817
})
1918
}else{
2019
res.setHeader('Content-Type',mime.lookup(url));
@@ -28,4 +27,12 @@ http.createServer(function(req,res){
2827
});
2928
}
3029

31-
}).listen(8080);
30+
}).listen(8080);
31+
32+
function change(url) {
33+
var reg = /([^?&=]+)=([^?&=]+)/g, obj = {};
34+
url.replace(reg, function () {
35+
obj[arguments[1]] = arguments[2];
36+
});
37+
return obj;
38+
}

‎listener.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ window.onload = function () {
55
xhr.open('post','/addUser');
66
xhr.onreadystatechange = function(){
77
if(this.readyState == 4 && this.status == 200){
8-
document.getElementById('usersDiv').innerHTML = this.responseText;
8+
var userList = JSON.parse(this.responseText);
9+
var users = '<ul>';
10+
userList.forEach(function(user){
11+
users+='<li>'+user['username']+'</li>';
12+
});
13+
users+='</ul>';
14+
document.getElementById('usersDiv').innerHTML = users;
915
}
1016
}
1117
xhr.send('username='+document.getElementById('username').value+'&password='+document.getElementById('password').value);

0 commit comments

Comments
 (0)
Please sign in to comment.