简体   繁体   中英

ajax: how to fetch data from database to html?

This application is developed with 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>

this is my knexfile.js:

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

and this is what i have in 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. 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. If you use express for this you can do something like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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