AJAX requests to Node.JS to update JSON file for Jquery-comments
NickName:Grant Campbell Ask DateTime:2016-08-03T04:01:12

AJAX requests to Node.JS to update JSON file for Jquery-comments

Jeez... How about that title, terrible lol.

Well I am using jquery-comments by Viima. (http://viima.github.io/jquery-comments/)

I'm trying to use ajax commands to a Node.JS script that will update a JSON file. All of which is local, no crossing domains or anything.

Here is my Node.JS script:

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {

fs.writeFile("/comments-data.json", "commentJSON", function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
});

}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');

Here is the Ajax post

                postComment: function(commentJSON, success, error) {
                        $.ajax({
                            type: 'post',
                            url: 'http://127.0.0.1:8080',
                            data: commentJSON,
                            success: function(comment) {
                                success(comment)
                            },
                            error: error
                        });
                },

I don't want to redirect. I just wanted to asynchronously show the new comment. Also this script will ultimately have to be able to handle video attachments as well and store the filepath inside the JSON file. But i believe, Jquery-comments just reads the file path from the JSON

Here is what the support site says for attachments

    uploadAttachments: function(commentArray, success, error) {
        var responses = 0;
        var successfulUploads = [];

        var serverResponded = function() {
            responses++;

            // Check if all requests have finished
            if(responses == commentArray.length) {

                // Case: all failed
                if(successfulUploads.length == 0) {
                    error();

                // Case: some succeeded
                } else {
                    success(successfulUploads)
                }
            }
        }

        $(commentArray).each(function(index, commentJSON) {

            // Create form data
            var formData = new FormData();
            $(Object.keys(commentJSON)).each(function(index, key) {
                var value = commentJSON[key];
                if(value) formData.append(key, value);
            });

            $.ajax({
                url: '/api/comments/',
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function(commentJSON) {
                    successfulUploads.push(commentJSON);
                    serverResponded();
                },
                error: function(data) {
                    serverResponded();
                },
            });
        });
    }
});

Copyright Notice:Content Author:「Grant Campbell」,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/38729450/ajax-requests-to-node-js-to-update-json-file-for-jquery-comments

More about “AJAX requests to Node.JS to update JSON file for Jquery-comments” related questions

AJAX requests to Node.JS to update JSON file for Jquery-comments

Jeez... How about that title, terrible lol. Well I am using jquery-comments by Viima. (http://viima.github.io/jquery-comments/) I'm trying to use ajax commands to a Node.JS script that will updat...

Show Detail

Node.JS server side script to update JSON file

I have partially written a NODE.JS file to update the JSON file with data received from the client. The post works successfully. The Get command does not. I was wondering if there's a better way to...

Show Detail

How to extend jquery-comments plugin?

I am using this jquery-comments plugin: https://github.com/Viima/jquery-comments/blob/master/js/jquery-comments.js I'd like to extend the plugin with my own functionality. According to this post,...

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

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

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

jQuery-comments plugin not running

There a exist a brilliant comments plugin that I would like to implement on my site. It can be found here https://github.com/Viima/jquery-comments. However I am having difficulty getting anything...

Show Detail

ajax requests to Node.js

In my app.js file in Node.js I have this: app.use(express.json()); which I thought was good enough but whenever I submit data via jQuery/Ajax the response is an empty object. After doing the below

Show Detail

Full ajax requests or more routes

I am planning to code my node.js web page. Im using node.js with express and jade templates. I want to make it fully asynchronized using POST AJAX requests to server. I meant that there will be o...

Show Detail

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