我在一个页面上有一个表单。我正在尝试提交该表单并将该数据发送到新页面。但是,response.render()不会呈现新页面,而是重新加载包含表单的页面。
页面,其中包含form = historicalReport.ejs, new page = historicalReportGraph.ejs
这是在historicalReport.ejs
中提交表单的代码$("#newHistoricalReportForm").validate({
rules: {
latitude: {
required: true
},
longitude: {
required: true
},
year: {
required: true
},
ppm_type: {
required: true
}
},
submitHandler: function(form) {
submitNewHistoricalReportForm();
}
});
function submitNewHistoricalReportForm() {
var form = document.getElementById("newHistoricalReportForm");
var year = form.elements[0].value;
var latitude = form.elements[1].value;
var longitude = form.elements[2].value;
var ppm_type = form.elements[3].value;
$.ajax({
type: 'POST',
dataType: 'html',
url: '/api/historicalGraph',
data: { latitude: latitude, longitude: longitude, year: year, ppm_type: ppm_type},
success: function (data) {
alert("The historical report will be displayed!");
},
error: function (data) {
alert("Something went wrong, please try again later!");
}
});
这是queries.js
中的代码app.post('/api/historicalGraph', function(request, response, next) {
const data = {
year: request.body.year,
latitude: request.body.latitude,
longitude: request.body.longitude,
ppm_type: request.body.ppm_type
};
response.render('pages/historicalReportGraph', {
year: data.year,
latitude: data.latitude,
longitude: data.longitude,
ppm_type: data.ppm_type,
email: request.user.email,
privilege: request.user.privilege
});
});