【发布时间】:2017-07-15 18:25:50
【问题描述】:
我需要制作一个按钮,点击后将我重定向到一个新页面(localhost:1337/LEDon)并将一些数据记录到控制台。但是,我无法单击按钮来加载新页面。
这是我的代码 -
app.js
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.sendFile(__dirname + '/view.html');
});
app.post('/LEDon', function(req, res) {
console.log('LEDon button pressed!');
res.sendFile(__dirname + '/success.html');
});
app.listen(1337);
clientside.js
$('#ledon-button').click(function() {
$.ajax({
type: 'POST',
url: 'http://localhost:1337/LEDon'
});
});
view.html
<!DOCTYPE html>
<head>
</head>
<body>
<button id='ledon-button'>LED on</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src='clientside.js'></script>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Success page</title>
</head>
<body>
LEDon button pressed!!
</body>
</html>
【问题讨论】:
-
有几种方法可以做到这一点,但如果代码和你在这里一样简单,你可以为你的 ajax 调用做一个
success:函数并将 window.location 设置为你的 URL我喜欢
标签: javascript ajax express