Permalink
Browse files

Add files,

    * node express ajax example code.
    * html static ajax file.
  • Loading branch information...
clonn committed Apr 3, 2012
1 parent e83992f commit 41d96385ef4245f99836189ea68b5aba47fcb1a5
Showing with 89 additions and 0 deletions.
  1. +47 −0 src/node_express_ajax_form.js
  2. +42 −0 src/view/express_ajax_example_form.html
@@ -0,0 +1,47 @@
+var http = require('http'),
+ url = require('url'),
+ fs = require("fs"),
+ qs = require('querystring'),
+ server;
+
+server = http.createServer(function (req,res) {
+ var urlData,
+ encode = "utf8",
+ filePath = "view/express_ajax_example_form.html",
+ formData,
+ action;
+
+ urlData = url.parse(req.url,true);
+ action = urlData.pathname;
+
+ if (action === "/Signup") {
+ formData = '';
+ req.on("data", function (data) {
+
+ formData += data;
+
+ });
+
+ req.on("end", function () {
+ var msg;
+
+ user = qs.parse(formData);
+ user.id = "123456";
+ msg = JSON.stringify(user);
+ res.writeHead(200, {"Content-Type":"application/json;","Content-Length":msg.length});
+ res.end(msg);
+ });
+ }
+ else {
+ fs.readFile(filePath, encode, function(err, file) {
+ res.writeHead(200, {"Content-Type":"text/html; charset=utf-8"});
+ res.write(file);
+ res.end();
+ });
+ }
+
+});
+
+server.listen(3000);
+
+console.log('Server跑起來了,現在時間是:' + new Date());
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Node.js菜鳥筆記(3)</title>
+ <link rel="stylesheet" href="/css/style.css" type="text/css" media="all" />
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ </head>
+ <body>
+ <h1>Node.js菜鳥筆記-註冊(用Ajax)</h1>
+ <form id="signup" action="/Signup" method="POST">
+ <label>使用者名稱:</label><input type="text" id="username" name="username" /><br>
+ <label>電子郵件:</label><input type="text" id="email" name="email" /><br>
+ <input type="submit" value="註冊我的帳號" /><br>
+ </form>
+ <div id="result">
+ </div>
+ <script type="text/javascript">
+ $(function(){
+ $('#signup input[type="submit"]').click(function(e){
+ var user = {};
+ user.username = $("#username").val();
+ user.email = $("#email").val();
+
+ $.post("/Signup",user,function(data){
+ greet(data)
+ });
+
+ e.preventDefault();
+ });
+
+ var greet = function(msg){
+ var resultNode = $('#result'),
+ greeting = $('<h2>Hi,'+msg.username+',你的會員id是:'+ msg.id +'我們會將會員啟動信寄至'+msg.email+'</h2>');
+
+ $('#signup').hide();
+ resultNode.html("");
+ resultNode.append(greeting);
+ };
+ });
+ </script>
+ </body>
+</html>

0 comments on commit 41d9638

Please sign in to comment.