《Response》.send( 出力する値 );
《Response》.json( JSON形式のデータ );
var str = req.query.input1;
res.json(
{ msg: str }
);
var callback = function(){
var target = document.getElementById("msg");
var res = JSON.parse(ajax.getResponse());
target.textContent = "you send::" + res.msg;
}※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
※routes/helo.js
var express = require('express');
var router = express.Router();
/* GET helo page. */
router.get('/', function(req, res, next) {
var p1 = req.query["p1"];
var p2 = req.query.p2;
var msg = p1 == undefined ? "" : p1 + "," + p2;
res.render('helo',
{
title: 'HELO Page',
msg: msg,
input: ''
}
);
});
/* POST helo page. */
router.post('/', function(req, res, next) {
var str = req.query.input1;
res.json(
{ msg: str }
);
});
module.exports = router;
※views/helo.ejs
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script type="text/javascript">
var ajax = null;
function doAction(){
var input = document.getElementById("input1");
var callback = function(){
var target = document.getElementById("msg");
var res = JSON.parse(ajax.getResponse());
target.textContent = "you send::" + res.msg;
}
ajax = new AjaxObject('/helo?input1=' +
input.value,callback);
}
function AjaxObject(url,callback){
var response = null;
var callback = callback;
ajaxStart(url);
this.getResponse = function(){
return response;
}
function ajaxStart(url){
var req = createRequest();
if (req == null){
alert("実行できません!");
return;
}
req.open("POST",url);
req.setRequestHeader
("User-Agent","XMLHttpRequest");
req.onreadystatechange = function(){
if (this.readyState == 4 &&
this.status == 200){
precallback(this);
}
}
req.send();
}
function createRequest(){
var httplist = [
function(){
return new XMLHttpRequest();
},
function(){
return new ActiveXObjct
("Msxml2.XMLHTTP");
},
function(){
return new ActiveXObject
("Microsoft.XMLHTTP");
}
];
for(var i = 0;i < httplist.length;i++){
try {
var http = httplist[i]();
if (http != null) return http;
} catch(e){
continue;
}
}
return null;
}
function precallback(request){
response = request.responseText;
callback();
}
}
</script>
</head>
<body onload="init()">
<h1><%= title %></h1>
<p id="msg"><%= msg %></a>
<form id="form1" name="form1" method="post" action="/helo"
onsubmit="return false;">
<input type="text" id="input1" name="input1"
value="<%= input %>">
<input type="button" id="btn" value="click"
onclick="doAction();">
</form>
</body>
</html>
| << 前へ | 次へ >> |