Check if there was POST with Javascript

Asked

Viewed 598 times

2

I have a singlepage application, and would like to monitor the POST for a webservice.

When I send the information it will remove the loading.

ex:

scope.deleteFile = function (fileId,index) {
                scope.loading = true;
                imageHandler.remove(scope.contentId, fileId);
                scope.attachments.splice(index, 1);
            };

That was the point where I stopped, however loading there it is true and knowing when or which function is jquery or native to javascript can I use to check if the post has been completed and after that give a scope.loading = false;

3 answers

2


If you are using jQuery to make Ajax calls to the server, Voce can use the events ajaxStart and ajaxComplete to execute its logic whenever an Ajax request is started or completed.

Example:

$(document).ajaxComplete(function(event, xhr, settings){
    console.log(settings.type);
    if(settings.type == "POST"){
        console.log("Foi um POST que acabou.");
    }
});
  • That’s really what I had in mind Renato. Thank you :)

1

In case you’re using jQuery, can add a custom header to all Ajax requests, using the following script syntax loaded right after jQuery load:

<script>
jQuery.ajaxSetup({headers: {"X-Requested-With":"XMLHttpRequest", "X-Ajax" : "1"}});
</script>

And you can access these values in the PHP controller through access to $_SERVER

  • in this case for my use it would be more complex since I use Jquery and Angular. It is an application in Java, but all the processing is done on the front end. Java is only responsible for services. But thanks for the help :)

  • You can recover the values in Java, as this is passed to each request for the controller

1

  • good, I’ll take a look at it worth!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.