Reputation: 53
Is it possible to complete a post request without redirecting or refreshing the page?
I am making a simple file browser in browser. The client side shows the USB stick content of the server. You know, when I open a folder, I send a string to server with the path data, so the server can send me the current folder's content. It is very disturbing that whenever I open a folder (click on that), the page is refreshing. Any ideas?
Upvotes: 2
Views: 3476
Reputation: 41
You can use Axios for post request without refreshing.
Link is below: https://axios-http.com/docs/post_example
Upvotes: 0
Reputation: 1
Basically, you need to request your server to let it know you want to fetch some data whenever a user opens a folder on its browser.
This operation is asynchronous and you definitely don't want your user's browser to refresh each time the server sends new data.
You could start implementing something like the following on a client-side script :
function fetchData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("GET", "api/fetch-folder-content", true);
xhttp.send();
}
Then, you'll have to write a route on the server (i.e., a fetch-folder-content GET route) and trigger the above function when you want to fetch fresh data from your server.
Upvotes: 0
Reputation: 104
if you use html to send your request to the server, the browser will follow the default behaviour of waiting for a response to your post command. If you want this to be treated differently, you might want to use AJAX as suggested. This way your browser will not reload the page, instead it will execute your code based on the server response.
Sometimes it's just a matter of disabling normal event browser behaviour like in event.preventDefault() in jquery.
If your question is different please post your code
Upvotes: 0
Reputation:
If you make an AJAX request which fires when you open the folder, the page will not refresh. You can set up a route on your server that will handle this request and return the content.
Upvotes: 1