简体   繁体   English

ajax:如何从数据库获取数据到html?

[英]ajax: how to fetch data from database to html?

This application is developed with node, bootstrap, knex. 此应用程序是使用node,bootstrap,knex开发的。 I need to fetch data from my mysql database "movedb" from the table Tab_Clienti(IDCliente, Cliente) to an html page called workspace.html , i'd like to use only javascript without php, and i need to put the fetched data into a <select> as a <option> 我需要从我的MySQL数据库“ movedb”中将数据从表Tab_Clienti(IDCliente,Cliente)提取到一个名为workspace.html的html页面中,我只想使用没有php的javascript,并且我需要将提取的数据放入<select>作为<option>

this is my knexfile.js: 这是我的knexfile.js:

module.exports = {
  client: 'mysql',
  connection: {
    user: 'root',
    password: '',
    database: 'movedb'
  }
}

and this is what i have in app.js: 这就是我在app.js中拥有的:

function post (path, data) {
  return window.fetch(path, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  })
}

I'm still learning develop web applications, so i apologize if there are some big mistakes. 我仍在学习开发Web应用程序,因此,如果有一些大错误,我深表歉意。 Thx for the support, if you have any questions or info to ask just ask me. 谢谢您的支持,如果您有任何疑问或要问的信息,请问我。

Like Chris G mentioned you need a way to serve page via HTTP. 就像Chris G提到的那样,您需要一种通过HTTP服务页面的方法。 If you use express for this you can do something like this: 如果为此使用express,则可以执行以下操作:

app.js app.js

var express = require('express');
var app = express();
var request = require('request');

var results = "some data";

app.get('/', function(req, res){
    res.render('index', {results : results});
})


app.listen(3000, 'localhost', function(){
    console.log("Server is running");
});

index.ejs index.ejs

<script>console.log(<%- JSON.stringify(results) %>);</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM