0

When doing simple server updates or submitting forms or that type of thing, I have come to a fork in the road...using AJAX:

var ajax = new XMLHttpRequest();
ajax.open("post", "myserver.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send('entry=' + encodeURIComponent(entry) + '&table=' + encodeURIComponent(table) + '&ID=' + encodeURIComponent(ID) + '&column=' + encodeURIComponent(column) + '&loc=' + encodeURIComponent(loc));
ajax.onload = function() {
    window.location = ajax.responseText.trim();
};

Or just a simple location and GETing the variables on the server:

window.location = myserver.php?entry=encodeURIComponent(entry)&&table=encodeURIComponent(table)&&ID=entry=encodeURIComponent(entry)&&column=encodeURIComponent(column)&&loc=encodeURIComponent(loc)

Granted, I am new to AJAX, but it seems to me that it does the same thing. I know GET and POST are processed differently on the server, but both get and post can be secured, so maybe I'm just missing something, because I know the simple location is not good for production code, but I don't know why.

2 Answers 2

2

The first example submits a POST request to a server and listens for a response. When the server responds, it sets window.location to the response text.

The second example constructs a URL within the client (browser) and sets window.location to that URL.

Setting window.location sets a value in the address bar; the browser will just load whatever URL is entered into the address bar.

Submitting a POST request via AJAX to a server will not attempt to load a new URL or do anything to the address bar; AJAX is typically used for submitting and/or requesting data from a server.

3
  • Ah, I think that's where I was confused. While you can still pass variables through both ways, the information travels differently. Thank you, that makes more sense now. Sep 2, 2017 at 16:40
  • 1
    Submitting a POST request to a server will not attempt to load a new URL or do anything to the address bar. Well, submitting a form would make the browser to change the actual location. AJAX doesn't because it performs the requests asynchronously.
    – Laiv
    Sep 2, 2017 at 17:52
  • 1
    @Laiv that should say Submitting a POST request via AJAX will not attempt to load a new URL or do anything to the address bar - I'll update for clarification Sep 2, 2017 at 19:06
1

To answer the specific question in the title...

Is there an advantage to using AJAX rather than a simple window.location?

The main advantage to Ajax is that you do the request without navigating the browser UI to a new page.

  • You don't disturb the user experience.
  • You don't take the time to load and render a new page and its resources.

You just do your Http request behind the scenes (from the user's perspective).

So, use Ajax when you want to do an Http request without navigating the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.