Reputation: 332
I'm trying to call a method uploadedImagesRefresh()
when the POST request is successful. The code works sometimes but sometimes it fails to call the uploadedImagesRefresh()
but I must say the backend is okay and the Post requests completes successfully.
function refreshUploadedImages() {//this
//function works quite well in other calls
}
$(function(){
//
$('.delete-img-btn').live('click', function() {
//asign the image id from the button attribute 'img-id'
var id= $(this).attr('img-id');
//The data to be send via ajax the server will recieve 2 POST variables ie. 'action' and 'id'(which is the img id)
var data={
'pk':id,
//call refreshUploadImages() upon succesful post
'success':refreshUploadedImages
};
//The ajax request.
$.post("/admin/image-uploader/delete/"+id , data);
});
});
What am I doing wrong in the above code?
Upvotes: 3
Views: 2420
Reputation: 119837
Add a callback. That would be the third parameter for $.post()
. That callback function gets called when the AJAX POST request is successful. In that function, you can call your function.
$.post("/admin/image-uploader/delete/"+id , data,function(data){
refreshUploadedImages()
});
You can even be straighforward, and make your function the callback instead:
$.post("/admin/image-uploader/delete/"+id , data, refreshUploadedImages);
Upvotes: 5