Why webpage refresh when we submit form with an image uploaded
Hello guys, I want a page to submit data without reloading. So I use the event.preventDefault() on my form and use the fetch API to send a request to my server. When I submit text fields only, like no image uploaded, I get the correct response and the web page isn't refresh. Now when I try to upload an image, I receive the response, a success response but the web page reloaded immediately, like as soon as response is sent, the web page is refresh, does someone know why please? The problem is surely because of the file upload because it only occur with the file being uploaded to the server.
31 Replies
here is the front-end script I'm using:
preserve the log and try again
im 300% sure you have a mistake somewhere that throws an exception
Hmmm preserve the log, is there an option in chrome or should write some code like logging everything in a file ?
it's an option in the f12
tools
oh didn't know we can preserve the log, thanks
here is what I obtain
there is no error status code though
This problem occur only when we try to upload an img file
here for e.g, I didn't upload any file from the file picker
and in the console?
hmm the console refreshes
preserve the log then
hmm
I'm not finding the option for this one :c
its under the tab list, on the left
ahh
wait
yeah got it, it navigates me to the current url
but why
good question
nothing stands out
i think u have an extra ')' after the profilePic
ooh wait nvm
i got it wrong
i am reading on mobile so the code is f-ed up
could it be due to the creation of blob?
idk why u r using blob here
to show the image
hmm i see
Faker, can you try commenting out that part and see if webpage refreshes or something?
also make sure u have the correct selector in the querySelector, #login
just to be sure
if it doesn't refresh after commenting out then u can try a different method for showing the image on the webpage. not sure how but i think FileReader class might help here
hmm even when I comment the blob thing, I still get the "navigated to url"
yep the selector is correct, the problem arises only with the file upload I don't understand, this is strange
can you put this somewhere where we can test?
euh I have a github
I never use it though, I don't really know how it works but I can try uploading the code there
the problem is the window, the browser is renavigating, like there is a redirection some default behaviour of the form I don't know
:c
it can be anything, but without actually seeing it, it's impossible to say
even chatgpt is not giving me an answer :c
you dont know enough to use chatgpt without being bamboozled
hmm I can paste the code here if you want
ohhh
ok I think I might know the error
Initially, I was using live server extension to open my html file, then from that port I was communicating to my back-end found on another port
Now the thing is each time I upload the file, behind the scenes in my back-end an upload folder is being created and the blob appended there
from what chatgpt has given me:
The VS Code Live Server plugin might reload the page when it detects file changes in your project directory. This could cause navigation even though your code is correct.
Which is correct, because in fact the problem of the page reloading, didn't arise the first time, this is because I created the upload directory outside of my project directoy but when I change the path and the upload folder was now in the project directory, this gives me the error
so I believe the problem was the live server extension being refreshed each time there is a change in the uploads folder
it sounds plausible, but i can't say
I believe that was the reason, instead of using live server, I just switch to my express server and works fine now... I should test by removing the upload folder in my project directory but will do that later on, this bug was really tiring :c
it does sound plausible, but i really have nothing to add or "debunk" from what you said