Skip to main content

jQuery how to make simple ajax call

In this tutorial, we will see how to make simple ajax call using jQuery. jQuery is widely used in almost all type of web development platform to make a flow of an application simpler and faster.

how to make simple ajax call using jQuery

AJAX these days, is very popular technique specially used when we don't want to reload site or want to avoid redirect after form submission or want to load specific section on the page. It is a very easy technique that can do many thing/tasks.
In this tutorial, we will learn to make an ajax call on form submit event. But to understand ajax, you need to have a basic understanding of JavaScript and html as we are going to use both the technologies to explain.
Please see below code carefully.

HTML Code

HTML file - registration.html
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
</head>

<body>
<form id="registration_form" name="registration_form" action="registration.php" method="POST">

   <label>Full Name: </label>
   <input type="text" name="full_name" id="full_name" placeholder="Please enter Name" />

   <label>Email: </label>
   <input type="text" name="email" id="email" placeholder="Please enter an Email" />

   <label>Mobile: </label>
   <input type="text" name="mobile" id="mobile" placeholder="Please enter Mobile" />

   <label>Address: </label>
   <textarea name="address" id="address" placeholder="Please enter an Address" /></textarea>

</form>


<script src="js/jquery.min.js"></script> <!-- Master jQuery to make the functions work -->
<script src="js/custom.js"></script> <!-- Custom js file for custom functions like ajax call etc -->

</body>
</html>

Custom JavaScript Code

JavaScript file - custom.js
/***** This code defines the ajax call *****/
$("#registration_form").submit(function(){
   $.ajax({
     url: $("#registration_form").attr("action"), /* gets the URL of form */
     data: $("#registration_form").serialize(), /* serialize function collects the form data */
     type: "POST", /* sets the form submission method */
     dataType: "JSON", /* this is useful to get the response in an ajax format */
     success: function(response){
        /* an action on the successful response */
     }, 
     error(xhr,status,error){
        /* an action on error */
     }
   });
});

In this way, we can make a call to the server page using AJAX to interact with it and get the task done without reloading the page. AJAX method is faster than the normal form submit as it only submits the form data and get the response but does not reload the page, hence no need to load all the files again which automatically make the action faster.

So, in this tutorial, we learnt how to make simple ajax call using jQuery. If you liked it, please share it with your friends.

Comments

Popular posts from this blog

CCAvenue avoid Error Code 21002 currency required parameter missing

In this tutorial, we will see how to avoid Error Code 21002 currency required parameter missing. This is a usual problem that occurs after connecting our application to the CCAvenue payment gateway. Due to some input values that are missing by developers while coding, such error occurs. CCAvenue is one of the leading online payment gateway service provider similar to other popular providers PayPal, Citrus, PayUMoney etc. Easy integration is an advantage of CCAvenue payment gateway. But, due to very minor mistakes in coding, developers get few errors while integrating CCAvenue with their application. CCAvenue have provided some guidelines of integration that are necessarily needed to be followed while integrating it into the application. Inputs values like amount, currency, merchant_id, redirect_url, cancel_url etc are minimum required and should be included in the form before sending the data to CCAvenue page. If we avoid them or forgot to use them into the code, CCAvenue th

CCAvenue avoid Error Code 21003 amount required parameter missing

In this tutorial, we will see how to avoid Error Code 21003 amount required parameter missing. This is another problem that occurs due to very minor mistake made by developer after connecting an application to the CCAvenue payment gateway. Due to some input values that are missing by developers while coding, such error occurs. CCAvenue is one of the leading online payment gateway service provider similar to other popular providers PayPal, Citrus, PayUMoney etc. Easy integration is an advantage of CCAvenue payment gateway. As per the guidelines of integration provided by CCAvenue that are necessarily needed to be followed while integrating it into the application. Input values like amount, currency, merchant_id, redirect_url, cancel_url etc are minimum required and should be included in the form before sending the data to CCAvenue page. If we avoid them or forgot to use them into the code, CCAvenue throws an error. As mentioned above, Amount is one of the required values that

CCAvenue avoid Error Code 21001 order id required parameter missing

In this tutorial, we will see how to avoid Error Code 21001 order_id required parameter missing. This is a usual problem that occurs after connecting our application to the CCAvenue payment gateway. But, it is not a big issue to solve. CCAvenue is one of the leading online payment gateway service provider similar to other popular providers PayPal, Citrus, PayUMoney etc. Easy integration is an advantage of CCAvenue payment gateway. But, due to very minor mistakes in coding, developers get few errors while integrating CCAvenue with their application. CCAvenue have provided some guidelines of integration that are necessarily needed to follow while integrating it into the application. It includes some parameters or inputs that are minimum required like amount, currency, merchant_id, redirect_url, cancel_url etc. If we avoid them or forgot to use them into the code, CCAvenue throws the error. As mentioned above Order Id is one of the required values that should be mentioned in co