how to get ajax response in jquery code example

Example 1: jquery ajax get

$.ajax({
    	url: "www.site.com/page",
    	success: function(data){ 
    	    $('#data').text(data);
    	},
    	error: function(){
    		alert("There was an error.");
    	}
    });

Example 2: ajax get js

let xhr = new XMLHttpRequest();

xhr.open("GET", "une/url");

xhr.responseType = "json";

xhr.send();

xhr.onload = function(){
    if (xhr.status != 200){ 
        alert("Erreur " + xhr.status + " : " + xhr.statusText);
    }else{ 
        alert(xhr.response.length + " octets  téléchargés\n" + JSON.stringify(xhr.response));
    }
};

xhr.onerror = function(){
    alert("La requête a échoué");
};

xhr.onprogress = function(event){
    if (event.lengthComputable){
        alert(event.loaded + " octets reçus sur un total de " + event.total);
    }
};

Example 3: jquery get request

jQuery get() Method
The jQuery get() method sends asynchronous http GET request to the server and retrieves the data.

Syntax:
$.get(url, [data],[callback]);
Parameters Description:

url: request url from which you want to retrieve the data
data: data to be sent to the server with the request as a query string
callback: function to be executed when request succeeds
The following example shows how to retrieve data from a text file.

Example: jQuery get() Method
$.get('/data.txt',  // url
      function (data, textStatus, jqXHR) {  // success callback
          alert('status: ' + textStatus + ', data:' + data);
    });
In the above example, first parameter is a url from which we want to retrieve the data. Here, we want to retrieve data from a txt file located at mydomain.com/data.txt. Please note that you don't need to give base address.

Example 4: jquery ajax responseText

// To get jquery AJAX calls working
// remember to add Jquery to your head tag:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

var response = null;
var responseTextValue = null;

$.ajax({
	type: "GET",   
	url: "<Your url text>",   
	async: false,
	success : function(data) {
		// Here you can specify that you need some exact value like responseText
		responseTextValue = data.responseText;
	    response = data;
	}
});

Tags:

Php Example