What is the AJAX success method?

AJAXAsynchronous JavaScript And XML is a set of web development techniques that uses many web technologies on the client side to create asynchronous web applications. With Ajax, web applications can send and retrieve data from a server, asynchronously, without interfering with the existing page’s display and behavior. AJAX is not a programming language – it requires a built-in browser with the XMLHttpRequest objectrequests data from the server, and it uses JavaScript and HTML DOM to display the data.

What is AJAX success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that’s called when a request proceeds.

The function takes three parameters, namely:

  1. The data returned from the server, formatted according to the dataType parameter, or the dataFilter callback function.

  2. A string describing the status.

  3. The jqXHR object.

The success method can take an array of functions as well. In this case, all functions would be called one by one.

Example

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url: "Test_success.txt", success: function(result){
      $("#newid").html(result);
    }});
  });
});
</script>
</head>
<body>

<div id="newid"><h2>Welcome to Jqeury</h2></div>

<button>New Button</button>

</body>
</html>

In the code above, the success method takes a result function as a parameter and executes once the request is successful. This request is caused when the New Button is clicked.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved