1$.ajax({
2 url: "www.site.com/page",
3 success: function(data){
4 $('#data').text(data);
5 },
6 error: function(){
7 alert("There was an error.");
8 }
9 });
1$.ajax({
2 type: "POST",
3 url: url,
4 data: data,
5 success: success,
6 dataType: dataType
7});
8
1$.ajax({
2 method: "POST",
3 url: "some.php",
4 data: { name: "John", location: "Boston" }
5})
6
7
1jQuery get() Method
2The jQuery get() method sends asynchronous http GET request to the server and retrieves the data.
3
4Syntax:
5$.get(url, [data],[callback]);
6Parameters Description:
7
8url: request url from which you want to retrieve the data
9data: data to be sent to the server with the request as a query string
10callback: function to be executed when request succeeds
11The following example shows how to retrieve data from a text file.
12
13Example: jQuery get() Method
14$.get('/data.txt', // url
15 function (data, textStatus, jqXHR) { // success callback
16 alert('status: ' + textStatus + ', data:' + data);
17 });
18In 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.
1
2let xhr = new XMLHttpRequest();
3
4xhr.open("GET", "une/url");
5
6xhr.responseType = "json";
7
8xhr.send();
9
10xhr.onload = function(){
11 if (xhr.status != 200){
12 alert("Erreur " + xhr.status + " : " + xhr.statusText);
13 }else{
14 alert(xhr.response.length + " octets téléchargés\n" + JSON.stringify(xhr.response));
15 }
16};
17
18xhr.onerror = function(){
19 alert("La requête a échoué");
20};
21
22xhr.onprogress = function(event){
23 if (event.lengthComputable){
24 alert(event.loaded + " octets reçus sur un total de " + event.total);
25 }
26};
1const xhttp = new XMLHttpRequest();
2xhttp.onload = function() {
3 document.getElementById("demo").innerHTML = this.responseText;
4}
5xhttp.open("GET", URL);
6xhttp.send();