I take this "json" from my application with this js:
$(document).ready(function() {
$("#submit").on('click', function(){
// send ajax
$.ajax({
url: 'http://127.0.0.1:8081/', // url where to submit the request
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : $("#form").serialize(), // post data || get data
success : function(result) {
// you can see the result from the console
// tab of the developer tools
console.log(result);
},
error: function(xhr, resp, text) {
console.log(xhr, resp, text);
}
})
});
});
on this server node:
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var obj = {
table: []
};
app.post("/", urlencodedParser, function(request, response) {
console.log(request.body); //This prints the JSON document received (if it is a JSON document)
obj.table.push(request.body);
});
var json = JSON.stringify(obj);
var fs = require('fs');
fs.writeFile('myjsonfile.json', json, 'utf8');
//Start the server and make it listen for connections on port 8080
app.listen(8081);
how to take the correct json file?