Change the Node.js GET /POST requests to the Ajax
NickName:Arefe Ask DateTime:2017-08-03T17:47:45

Change the Node.js GET /POST requests to the Ajax

I'm a beginner in JavaScript and working to make the same request from some Node.js backend code to by using the Ajax based GET/ POST requests. I use MySQL database. I have gone through some tutorials and get some understanding. For example, an Ajax POST request could be something like,

    $.ajax({

            url: '/walletinfo',
            method: 'POST',
            data: {
                name: addressName,
                address: wallet.getAddress()
            },
            success: function(result) {
                console.log(result)
            },
            error: function(err) {
                console.log(err)
            }
        }) 

The Node.js POST requests that I have is provided,

// initiations of the code 
const express = require('express')
const path = require('path')

const bodyParser = require("body-parser");

const mysql = require('mysql');
const app = express()

const con = mysql.createConnection({

    host: "localhost",
    user: "testuser",
    password: "testpassword",
    database : 'wallet1'
});

con.connect(function(err) {

  if (err) throw err;
  console.log("Connected!");
});


app.use('/', express.static(path.join(__dirname, 'public')))
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


// initiations of the code
app.post('/walletinfo', function (req, res) {

    var sql = "INSERT INTO wallet_info (name, address) VALUES ('" + req.body.name +"', '" + req.body.address + "')";

    con.query(sql, function (err, result, fields) {
        if (err) console.error(err);

        res.json(result)
  });
})

So, its seems that the req object build-up the SQL query and the con.query tries to execute it. If the INSERTION is done as expected, we embedded the res with the result.

It seems I have to do something with the data: { name: addressName, address: wallet.getAddress() } part of the $.ajax request and make the con.query call over there.

I also have a GET request to change from Node to the Ajax,

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

    con.query("SELECT * FROM wallet_info", function (err, result, fields) {

        if (err) throw err;

        console.log(result);
        res.json(result)
  });
})

How to do it properly?

Copyright Notice:Content Author:「Arefe」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45480631/change-the-node-js-get-post-requests-to-the-ajax

More about “Change the Node.js GET /POST requests to the Ajax” related questions

Change the Node.js GET /POST requests to the Ajax

I'm a beginner in JavaScript and working to make the same request from some Node.js backend code to by using the Ajax based GET/ POST requests. I use MySQL database. I have gone through some tutori...

Show Detail

Multiple Ajax Requests to Node.js timing out

I have a very unique setup, which I imagine is having some effect on this issue (which I've never seen before). I'm writing a Google Chrome Extension, and I'm relying on Node.js as my backend. Wh...

Show Detail

node.js jquery - .post ajax like php

I am coming from PHP where an AJAX call from jQuery is done like this. html: $('a').click(function(){ var mydata = 'abc123'; $.post( 'process.php', { mydata:mydata}, function(data){ ...

Show Detail

Ajax requests with Node.js

After looking around google and stack overflow, I'm yet to find a way to post data to my Node.js server. There seems to be disparity between the methods I've seen, presumably due to depreciation. ...

Show Detail

AJAX requests via GET or POST?

I'm doing some AJAX requests that insert new records into a database via POST, to follow the REST methods. However, from time to time I'm getting some errors on the server (using Apache & Djan...

Show Detail

What is the limit of sending concurrent ajax requests with node.js?

I am making a node.js server that makes a lot of ajax requests to download json or html code, to different websites (not ddos/dos). For this language, I was wondering how many requests I can make a...

Show Detail

A lot of ajax POST requests

I have a site with a lot of post requests (if needed I can change to GET) for now ajax is making 1 request per second per opened tab per user. So 20 users online = 20 requests/s + normal requests t...

Show Detail

Which method HTTP GET/POST used by AJAX requests?

Which method HTTP GET/POST used by AJAX requests? And can we specify the method while making Ajax call?

Show Detail

Access AJAX POST data with Node.js/Express

I'm building a web application that uses Node.js/Express for the backend. In my front end, I am sending an AJAX request to the server through Javascript that looks likes this: var xhttp = new

Show Detail

Sending GET and POST AJAX requests at the same time

i just would like to know if it is possible sending GET and POST AJAX requests at the same time and if it is how to do it using the XMLHttpRequest object. Thanks all for helping :D

Show Detail