upvotes and attachments problem
Closed this issue · 1 comments
ladysilvia commented
I'm using json-server, I have no problems with Get, Post and Put.
it does not work upvoteComment and uploadAttachments.
Please can you help me.
Thank you so much
getUsers: function(commentJSON, success, error) {
$.ajax({
type: 'get',
url: 'http://localhost:3000/usersArray',
data: commentJSON,
success: function(commentJSON) {
success(commentJSON)
},
error: error
});
},
getComments: function(success, error) {
$.ajax({
type: 'get',
url: 'http://localhost:3000/commentsArray',
success: function(commentsArray) {
success(commentsArray)
},
error: error
});
},
postComment: function (commentJSON, success, error)
{
$.ajax({ type: 'post',
url: 'http://localhost:3000/commentsArray',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(commentJSON),
success: setTimeout(function () {
success(commentJSON); }, 500),
error: error
});
},
putComment: function(commentJSON, success, error) {
$.ajax({
type: 'put',
url: 'http://localhost:3000/commentsArray/' + commentJSON.id,
data: commentJSON,
success: function(commentJSON) {
success(commentJSON)
},
error: error
});
},
deleteComment: function(commentJSON, success, error) {
$.ajax({
type: 'delete',
url: 'http://localhost:3000/commentsArray/' + commentJSON.id,
success: success,
error: error
});
},
upvoteComment: function(commentJSON, success, error) {
var commentURL = '/comments/' + commentJSON.id;
var upvotesURL = commentURL + '/upvotes/';
if(commentJSON.userHasUpvoted) {
$.ajax({
type: 'post',
url: upvotesURL ,
data: {
comment: commentJSON.id
},
success: function() {
success(commentJSON)
},
error: error
});
} else {
$.ajax({
type: 'delete',
url: upvotesURL + commentJSON.id,
success: function() {
success(commentJSON)
},
error: error
});
}
},
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: 'http://localhost:3000/comments/image/',
type: 'post',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(commentJSON) {
successfulUploads.push(commentJSON);
serverResponded();
},
error: function(data) {
serverResponded();
},
});
});
},
jessenieminen commented
Unfortunately we can't provide you with support regarding the implementation of your backend.