Skip to content

Instantly share code, notes, and snippets.

@karliky
Created October 7, 2014 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karliky/40913ad2463417e4a444 to your computer and use it in GitHub Desktop.
Save karliky/40913ad2463417e4a444 to your computer and use it in GitHub Desktop.
Canvas+ CocoonJS Post Test
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function CAsyncDBRequest(phpScript, postData, parseObject, parseFn) {
var self = this;
this.mparseObject = parseObject;
this.mparseFn = parseFn;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (self.m_parseFn != null) {
if (self.m_parseObject != null) {
self.m_parseObject[self.m_parseFn].call(self.m_parseObject, xmlhttp.responseText);
} else {
global[self.m_parseFn].call(global, xmlhttp.responseText);
}
}
}
}
xmlhttp.open("POST", "http://172.16.100.20:8080", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var postString = "";
for (var i = 0; i < postData.length; ++i) {
if (i > 0) {
postString += "&";
}
postString += postData[i][0] + "=" + encodeURIComponent(postData[i][1]);
}
console.log("PostString=" + postString);
xmlhttp.send(postString);
}
// Call to the utility function
new CAsyncDBRequest("dbsessionlogin", [
['sessionId', "testsessionid"]
], null, 'loginResponse');
function loginResponse(str) {
console.log(str);
}
</script>
</head>
<body>
</body>
</html>
http = require('http');
fs = require('fs');
server = http.createServer( function(req, res) {
console.dir(req.param);
if (req.method == 'POST') {
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function () {
console.log("Body: " + body);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
}
else
{
console.log("GET");
var html = fs.readFileSync('index.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}
});
port = 8080;
host = '172.16.100.20'; // Change this with your local IP
server.listen(port, host);
console.log('Listening at http://' + host + ':' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment