我在单击应用程序上的按钮时尝试执行mysql查询而不刷新页面。当我点击我的"更多信息"我在我的Chrome浏览器上收到502坏网关日志按钮。 注意:以下部分代码用于Michael Cheng的YouTube视频。
我的代码:
staticPricing.js
$(function() {
// GET/READ info FROM server
$('#getInfoMats-button').on('click', function () {
// ajax GET request
$.ajax({
url: '/materials',
contentType: 'application/json',
success: function (response) {
console.log(response);
var tbodyEl = $('#tbodytest');
tbodyEl.html('');
response.result.forEach(function (result) {
tbodyEl.append('\
<tr>\
<td>' + result.PricePerSheetORFt + '</td> \
</tr>\
');
});
}
});
});
pricingTable.js
// sql handler
exports.sqlGetMaterialPricing = "SELECT PricePerSheetORFt FROM genericname.info_pricing WHERE 'Key' = ?;";
exports.getMaterial = function(req, res) {
var weErrored = false;
var post = {
PricePerSheetORFt: req.body.PricePerSheetORFt
};
mySqlController.connection.query(exports.sqlGetMaterialPricing, post, '1',
function(err, result, fields){
if (err) {
console.log(err.code);
weErrored = true; // setting the error to true, this is used to display a message on the front end
}
res.send(JSON.stringify(result));
});
}
路线
var pricingTable = require('../controllers/pricingTable');
app.get('/materials', pricingTable.getMaterial);
和
pricingTable.hbs
<script src="staticPricing.js"></script>
<tbody id="tbodytest">
{{#each data}}
<tr>
<td>{{this.IGNORE}}</td>
<td>{{this.IGNORE}}</td>
<td>{{this.IGNORE}}</td>
<td>
<p>
<button id="getInfoMats-button" class="btn btn-outline-secondary" data-toggle="collapse" data-target="#collpase{{this.Key}}" aria-expanded="false" aria-controls="collapseExample">
More Info
</button>
</p>
</td>
</tr>
<tr>
<td colspan="4">
<div class="collapse" id="collapse{{this.Key}}">
<div class="card card-block">
<div class="row">
<div class="col">
<p><strong>PricePerSheetORFt:</strong> {{this.PricePerSheetORFt}}</p>
</div>
</div>
</div>
</div>
</td>
</tr>
{{/each}}
</tbody>
请不要介意这个。签名,我改变了这个问题。