get() Method for ajax event - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:get

Description

The $.get() method loads data from the server using a HTTP GET request.

Syntax

$.get(URL,data,function(data,status,xhr),dataType)
Parameter RequireDescription
URL Required.URL to request
data Optional.data to send to the server along with the request
function(data,status,xhr) Optional. a function to run if the request succeeds
dataType Optional. data type of the server response. By default jQuery would do the guess.

function(data,status,xhr) parameters:

  • data - resulting data from the request
  • status - status of the request ("success", "notmodified", "error", "timeout", or "parsererror")
  • xhr - XMLHttpRequest object

Possible types for dataType:

  • "xml" - An XML document
  • "html" - HTML text
  • "text" - A plain text string
  • "script" - Runs the response as JavaScript, and returns it as plain text
  • "json" - Runs the response as JSON, and returns a JavaScript object
  • "jsonp" - Loads in a JSON block using JSONP.

Examples

Request "test.php" and send some additional data along with the request (ignore return results):

$.get("test.php", { name:"Mary", town:"England" });

Request "test.php" and pass arrays of data to the server (ignore return results):

$.get("test.php", { 'colors[]' : ["Red","Green","Blue"] });

Request "test.php" and alert the result of the request:

$.get("test.php", function(data){
  console.log("Data: " + data);
});

The following code shows how to request "test.php", but ignore return results:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.get("test.php");
    });/* w  w  w  .  j  a v a  2s. com*/
});
</script>
</head>
<body>

<button>test</button>

</body>
</html>

Related Tutorials