gpt4 book ai didi

mysql - Node.js - 如何通过 AJAX 传递多个 MySQL 查询参数

转载 作者:行者123 更新时间:2023-11-29 10:44:59 24 4
gpt4 key购买 nike

我正在尝试完成以下函数,该函数通过 AJAX 将值作为查询参数发送到后端,该查询将在用户屏幕上输出报告结果而无需重新加载。

如果我只使用一个参数(日期报告:'2017-06-21')它工作正常并且我在同一页面上获得结果而无需重新加载它。

查看网址为localhost/reports

但是,当我尝试使用像 currencyreport 这样的第二个值时,直到 AJAX 函数和后端才得到它。

EJS

<input id="datereport" class="form-control" name="datereport" value="2017-06-21">
<input id="currencyreport" class="form-control" name="currencyreport" value="GBP">

<input type="button" value="Search" class="btn btn-info mr-1" onClick="searchReport([document.getElementById('datereport').value,document.getElementById('currencyreport').value])">

用于向后端发送和获取值的 AJAX 函数

function searchReport(datereport, currencyreport) {

console.log(datereport) // it outputs both datereport,currencyreport values instead of the first one only ["2017-06-21","GBP"]
console.log(currencyreport) // it outputs undefined
$.ajax({
url: "/reports",
type: "get",
data: $("#report-form").serialize(),
success: function(res) {
$.post("/reportsdate/" + data, function(data) {
// it builds GET /reports?datereport=2017-06-21&currencyreport=GBP
ok(data);
});
return false;
},
error: function(xhr, status, error) {
// error stuff
}
});
}

// The part which takes the backend results and output in the same page without reloading

function ok(datares) {
document.getElementById("results").innerHTML = Object.values(datares.data[0])
}

后端

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var url = require('url');

router.use(bodyParser.urlencoded({ extended: true }));
router.use(function(req, res, next) {
console.log(req.method, req.url);
next();
});


var reports = router.route('/reports');
var reportsdate = router.route('/reportsdate');

router.get('/reportsdate', function(req, res) {
res.render('reports');
});

router.get('/reports', function(req, res) {
res.render('reports');
});

reportsdate.post(function(req, res, next) {

datereport = req.query.datereport;
currencyreport = req.query.currencyreport;

req.getConnection(function(err, conn) {

if (err) return next("Cannot Connect");

var queryString = "SELECT SUM(total_profit) AS fee_total FROM transactions WHERE date_created = ?";
var queryParams = [];
queryParams.push(datereport);

if(currencyreport){
queryString += ' AND currency = ? ';
queryParams.push(currencyreport);
}

var query = conn.query(queryString, queryParams, function(err, rows) {
if (err) {
console.log(err);
return next(err);
}
var ab = {data: rows};
console.log('hohohoho ' + JSON.stringify(ab));
res.send(ab)

});
});
});

router.get('/reports', function(req, res) {
res.render('reports', {data: ab});
});

最佳答案

您正在使用:

onClick="searchReport([document.getElementById('datereport').value,document.getElementById('currencyreport').value])">

这意味着您使用数组调用 searchReport。但你的函数只需要变量。

所以,如果你去掉大括号,一切都会正常。

onClick="searchReport(document.getElementById('datereport').value,document.getElementById('currencyreport').value)">

关于mysql - Node.js - 如何通过 AJAX 传递多个 MySQL 查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com