How to route using XMLHttpRequest (js) and "pure" NodeJS? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 4

How to route using XMLHttpRequest (js) and "pure" NodeJS?

I am setting up a server using Node and hosting a HTML file "index.html" with "script.js" on localhost. My client-side code looks like: https://code.sololearn.com/WP3yos46EDwb/#html And, I'm handling the ajax request using my server-side script "server.js" which I've learnt from https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework#Example - It is really a good source :) After doing all these things, my hosted site runs very well. But, the only problem I face is routing programmatically. When, I type localhost:8125/login.html - it routes exactly as I wished but, when I try the same through my ajax request. It does everything, EXCEPT routing :| Please help me to solve this problem. Thanks in advance!

25th Dec 2018, 7:00 AM
777
777 - avatar
6 Antworten
+ 2
You can use ajax to fetch some resources or information from the server and put it dynamically in current page. For example changing or updating a datalist. But if you want an entire page, just use link
25th Dec 2018, 8:15 AM
Taste
Taste - avatar
+ 2
Why not make it as a link ?
25th Dec 2018, 7:31 AM
Taste
Taste - avatar
+ 2
Thanks Taste, it works! But I want to learn AJaX and so I was trying to route using AJaX. Anyways, if AJaX isn't the proper method to route, then I can use links.
25th Dec 2018, 8:12 AM
777
777 - avatar
+ 2
@Taste, can I bother you once more? I need help with redirection (changing the URL of web page programmatically, similar to routing) using Node.js In case of redirection, I can't use links, so how can I achieve it?
27th Dec 2018, 11:47 AM
777
777 - avatar
+ 2
This one if i remember correctly, i've been using express for too long ­čśé res.writeHead(statusCode,{"location":"dest"}) statuscode should be 301 or 302 for temporary redirct or permanent
27th Dec 2018, 12:00 PM
Taste
Taste - avatar
+ 2
@Taste Thanks for your help :)
27th Dec 2018, 2:18 PM
777
777 - avatar