Ajax, call jQuery POST to node.js expressjs

tipjs,object,es6,nodejs,ajax

#Client HTML

$(function(){				
    $('#select_link').click(function(e){
        e.preventDefault();
        console.log('select_link clicked');
        
         /*$.ajax({
            dataType: 'jsonp',
            data: "data=yeah",						
            jsonp: 'callback',
            url: 'http://localhost:3000/endpoint?callback=?',						
            success: function(data) {
                console.log('success');
                console.log(JSON.stringify(data));
            }
        });*/

        var data = {};
        data.title = "title";
        data.message = "message";
        
        $.ajax({
            type: 'POST',
            data: JSON.stringify(data),
            contentType: 'application/json',
            url: 'http://localhost:3000/endpoint',						
            success: function(data) {
                console.log('success');
                console.log(JSON.stringify(data));
            }
        });

        /*$.ajax('http://localhost:3000/endpoint', {
                type: 'POST',
                data: JSON.stringify(data),
                contentType: 'application/json',
                success: function() { console.log('success');},
                error  : function() { console.log('error');}
        });*/
    });				
});

Back End Nodejs

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

app.use(express.bodyParser());

/*app.get('/endpoint', function(req, res){
	var obj = {};
	obj.title = 'title';
	obj.data = 'data';
	
	console.log('params: ' + JSON.stringify(req.params));
	console.log('body: ' + JSON.stringify(req.body));
	console.log('query: ' + JSON.stringify(req.query));
	
	res.header('Content-type','application/json');
	res.header('Charset','utf8');
	res.send(req.query.callback + '('+ JSON.stringify(obj) + ');');
});*/

app.post('/endpoint', function(req, res){
	var obj = {};
	console.log('body: ' + JSON.stringify(req.body));
	res.send(req.body);
});


app.listen(3000);

Có thể bạn đã miss một số snippets code

Remove item in array javascript es6

Những bạn nào mà đã sử dụng ES6 một thời gian rồi thì có thể sử dụng method array.filter()

Spread Operator for Objects

Using the spread operator during an Object declaration will assign the properties of the referenced Object to the new Object.

Async await in javascript

Async await in javascript

Converting Object to an Array

Converting Object to an Array

using Typescript callback style error handling