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 url: 'ajaxfile.php',
3 type: 'post',
4 data: {name:'yogesh',salary: 35000,email: 'yogesh@makitweb.com'},
5 success: function(response){
6
7 }
8});
1//Change the text of a <div> element using an AJAX //request:
2//using JQuery
3
4
5$("button").click(function(){
6 $.ajax({url: "demo_test.txt", success: function(result){
7 $("#div1").html(result);
8 }});
9});
10
11
12
13//To send a request to a server, we use the open() //and send() methods of the XMLHttpRequest object:
14// Javascript
15
16
17xhttp.open("GET", "ajax_info.txt", true);
18xhttp.send();
19
20//example below
21<html>
22<body>
23
24<h1>The XMLHttpRequest Object</h1>
25
26<button type="button" onclick="loadDoc()">Request data</button>
27
28<p id="demo"></p>
29
30
31<script>
32function loadDoc() {
33 var xhttp = new XMLHttpRequest();
34 xhttp.onreadystatechange = function() {
35 if (this.readyState == 4 && this.status == 200) {
36 document.getElementById("demo").innerHTML = this.responseText;
37 }
38 };
39 xhttp.open("GET", "demo_get.asp", true);
40 xhttp.send();
41}
42</script>
43
44</body>
45</html>
1$.ajax({
2 type: 'POST',
3 url: "<Your URL>",
4 contentType: 'application/json; charset=utf-8'
5 // Set your dataType to either 'html' or 'text'.
6 // Keep in mind: dataType is for receiving,
7 // contentType is for sending
8 dataType: 'html',
9 data: { example: 1, id: "0x100"},
10 // Note: the data above is used in sending,
11 // data below is a variables that stores received data
12 success: function (data){
13 // Suppose you have an html element, where you want to append
14 // the response:
15 $('#<Your html element id>').html(data);
16 // .html(data) overwrites existing data
17 // Use .append(data) to add response without overwriting!
18 }
19});
20
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();