Sending information from a JavaScript site to the NODE.js server

0

Well, I have a site in javascript where I get information from a Rest API (json)

I would like to get this information to my server (A node.js that I created that connects to the database)

It's my first time working with web development, would you like to know how to make the connection between the two?

My front end

<!DOCTYPE html>
<html>

    <head>
        <style>     
            .bodyFrame {
                margin: 40px;
            }

            .headerLabel {
                font-weight: bold;
            }

        </style>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><body><divclass="bodyFrame">
            <h2 style="text-align:center;">WIDS JSON Retrieval Example</h2>

            <button type="button" onclick="makeServiceCall()">Retrieve JSON Data</button>
            <br /><br />

            <label class="headerLabel">Programs</label>
            <ul id="programUL"></ul>
        <div>

        <script>

            function makeServiceCall() {                    
                var url = "http://widsservicedev.yaharasoftware.com/WidsService/JSON/GetPortagePrograms/?apikey=104043F0-9C24-4957-879D-046868973CC4&callback";

                $.getJSON(url, function (data) {
                    //var myArray = [];
                    //myArray[0] = data;
                    parseProgramData(data, url);
                });                 
            }

            function parseProgramData(jsonData, url) {      

                $("#dataHeader").empty();
                $("#dataHeader").append('<b>' + url + '</b>');

                var programUL = document.getElementById("programUL");       

                for (var pgmIndex = 0; pgmIndex < jsonData.Programs.length; pgmIndex++) {                   
                    var pgmLi = document.createElement("li");
                    var program = jsonData.Programs[pgmIndex];
                    var programInfoRevision = program.ProgramInfoRevisions[0];
                    var numberTitle = programInfoRevision.ProgramNumber + " " + programInfoRevision.ProgramTitle;
                    pgmLi.appendChild(document.createTextNode(numberTitle));
                    programUL.appendChild(pgmLi);                   

                    var linebreak = document.createElement("br");
                    pgmLi.appendChild(linebreak);

                    var poLabel = document.createElement("label");
                    poLabel.appendChild(document.createTextNode("Program Outcomes"));
                    poLabel.classList.add("headerLabel");                   
                    pgmLi.appendChild(poLabel);                 

                    var pgmOutcomeUL = document.createElement("UL");
                    pgmLi.appendChild(pgmOutcomeUL);

                    for (var poIndex = 0; poIndex < program.ProgramOutcomes.length; poIndex++) {                    
                        var poLi = document.createElement("li");
                        poLi.appendChild(document.createTextNode(program.ProgramOutcomes[poIndex].Description));
                        pgmOutcomeUL.appendChild(poLi);
                    }                   
                }

            }
        </script>


    </body>

    <footer>        
    </footer>
</html>

My node.js

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

app.get('/', function (req, res) {

    var sql = require("mssql");

    // config for your database
/*    var config = {
        user: 'papercut',
        password: 'Portage.2018',
        server: 'devsqlcl2:1433', 
        database: 'AgrM6',
        port: "1433",
        dialect:",ssql",
        dialectOptiond:"SQLEXPRESS"
    };*/

    // connect to your database
    sql.connect('mssql://xxx:xxxx@xxxx:1433/xxx', function (err) {

        if (err) console.log(err);

        // create Request object
        var request = new sql.Request();

        // query to the database and get the records
        request.query('select * from dbo.balance_papercut', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});

var server = app.listen(5000, function () {
    console.log('Server is running..');
});

Thank you.

    
asked by anonymous 31.10.2018 / 16:27

1 answer

1

The verb get only retrieves data from the server, you want to send data in the opposite direction, you have to send via POST.

On the node you will need the following:

Install the body-parser via npm then add the following configuration in your server.js before the app.get (...:

var bodyParser = require('body-parser');
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 

Now replace the app.get with

app.post('/', function(req, res) {
    var body = req.body;


});

In the variable body vc will have json sent from the client

Now on the client side you need to send via Ajax POST as in the example below:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
    
31.10.2018 / 17:02