Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) AJAX Concepts GET and POST

GET/POST with AJAX

So I get why you would want to use Post for sensitive info (Username/password displaying in the web browser)

I am wondering though if you send a GET request with Ajax, since the whole point is to not reload the browser, does any of that info still show up in browser?

I'm sure there is just best practices associated with it, but I was just wondering if theres another level or protection if using Ajax requests rather than reloading the web page.

1 Answer

Hi Alex Henricks,

Here's an example of a get request:

$.get("demo_test.asp", function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });

A GET request is sent to "demo_test.asp", it returns data with a status code.

Here's an example of a post request:

 $.post("demo_test_post.asp",
    {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });

Again we first write the address to which we want to send our request, then we specify the data we want to send in JSON, then we have a function which receives the response data from the server with a status code.

The two methods posted above are shorthand for the longer notation which goes like this:

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
  success: function(data,  textStatus,  jqXHR) {
                alert("Data: " + res + "\nStatus: " + textStatus);
            }
})
$.ajax({
  method: "GET",
  url: "some.php",
  success: function(data,  textStatus,  jqXHR) {
                alert("Data: " + res + "\nStatus: " + textStatus);
            }
})

Method, Url, Success are just a few of the keys you can use, check out the documentation for more on this.

Good luck!