AJAX请求Node.JS更新JQuery文件的JQuery评论
Jeez …那个头衔怎么样,可怕的大声笑。
那么我正在使用Viima的jQuery评论。 ( http://viima.github.io/jquery-comments/ )
我正在尝试使用ajax命令来更新一个JSON文件的Node.JS脚本。 所有这些都是本地的,没有跨域或任何东西。
这是我的Node.JS脚本:
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/');
这里是Ajax文章
postComment: function(commentJSON, success, error) { $.ajax({ type: 'post', url: 'http://127.0.0.1:8080', data: commentJSON, success: function(comment) { success(comment) }, error: error }); },
我不想redirect。 我只是想asynchronous显示新的评论。 此外,这个脚本最终还必须能够处理video附件,并将文件path存储在JSON文件中。 但是我相信,Jquery-comments只是从JSON中读取文件path
以下是支持网站对附件所说的内容
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(); }, }); }); } });