JavaScript AJAX Basics (retiring) jQuery and AJAX Posting Data with jQuery

raj kanwar
raj kanwar
4,043 Points

What is the purpose of response included in the function which runs inside jquery post method at the end?

What is the purpose of response included in the function which runs inside jquery post method at the end?

Could you paste the code that you're referring to?

2 Answers

Hi Raj,

Ajax functions usually have a 'callback' function which will tell you if there is a success or failure, and then allows you to execute a script in case of failure.

Jeremiah Lugtu
Jeremiah Lugtu
9,910 Points

in function parameters, names are not strictly defined. You can name it whatever you want, so long as you understand the purpose of it.

In this case, it's an AJAX callback, the parameter of 'response' within the callback's anonymous function 'function(response)' is the jQuery equivalent of responseText, when creating AJAX requests using vanilla JS... responseText or response is what will happen when the AJAX Request is processed/completed:

$('#signup').html("<p>Thanks for signing up!</p>")

Remember that jQuery is simply a library, every jQuery API has a corresponding JS structure

In the case of jQuery AJAX callback functions... Usually you will see 'response' or 'data' within the callback function parameter

eg

$.get(url, data, function(data){

}

but generally, you can name it anything you want

eg

$.post(url, data, function(awesome){

}