Created
May 9, 2010 17:13
-
-
Save polotek/395282 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.addListener('uncaughtException', function(err) { | |
throw err; | |
}); | |
var sys = require('sys') | |
, fs = require('fs') | |
, http = require('http'); | |
http.createServer(function (req, res) { | |
sys.debug(req.url); | |
req.setEncoding('utf8'); | |
if(/ajax/.test(req.url)) { | |
var t = JSON.stringify({id:"test"}); | |
res.writeHead(200, { | |
'Content-Length': t.length | |
, 'Content-Type': 'text/json' | |
}); | |
res.write(t); | |
res.end(); | |
} else { | |
fs.readFile('test.html', 'utf8', function(err, data) { | |
if(err) { | |
res.writeHead(500, {}); | |
res.end(); | |
throw err; | |
} | |
res.writeHead(200, { | |
'Content-Length': data.length | |
, 'Content-Type': 'text/html' | |
}); | |
res.write(data); | |
res.end(); | |
}); | |
} | |
}).listen(8000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Page title</title> | |
</head> | |
<body> | |
<h1>Test</h1> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> | |
<script type="text/javascript"> | |
$(function() { | |
$.getJSON('http://localhost:8000/ajax?' + (new Date()).getTime(), function(data, textStatus) { alert(data, textStatus); } ); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment