javascript ajax request code example

Example 1: ajax syntax in javascript

var id = empid;

$.ajax({
    type: "POST",
    url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
    data: "{empid: " + empid + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result){
        alert(result.d);
        console.log(result);
    }
});

Example 2: $.ajax javascript

$.ajax({
    url: 'https://example.com/your-page',
    success:function(data){
        //'data' is the value returned.
    },
    error:function(){
        alert('An error was encountered.');
    }
});

Example 3: how to make ajax request javascript

//Change the text of a <div> element using an AJAX //request:
//using JQuery


$("button").click(function(){
  $.ajax({url: "demo_test.txt", success: function(result){
    $("#div1").html(result);
  }});
});



//To send a request to a server, we use the open() //and send() methods of the XMLHttpRequest object:
// Javascript


xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();

//example below
<html>
<body>

<h1>The XMLHttpRequest Object</h1>

<button type="button" onclick="loadDoc()">Request data</button>

<p id="demo"></p>


<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "demo_get.asp", true);
  xhttp.send();
}
</script>

</body>
</html>

Example 4: javascript ajax request

function makeRequest (method, url, data) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    if(method=="POST" && data){
        xhr.send(data);
    }else{
        xhr.send();
    }
  });
}

//GET example
makeRequest('GET', "https://www.codegrepper.com/endpoint.php?param1=yoyoma").then(function(data){
              var results=JSON.parse(data);
});
//POST example
var data={"person":"john","balance":1.23};
makeRequest('POST', "https://www.codegrepper.com/endpoint.php?param1=yoyoma",data).then(function(data){
              var results=JSON.parse(data);
});

Example 5: ajax open a request

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  
  //looking for a change or state , like a request or get.
  xhttp.onreadystatechange = function() {
     
     //if equal to 4 means that its ready.
    // if equal to 200 indicates that the request has succeeded.
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  
  //GET method for gettin the data // the file you are requesting
  xhttp.open("GET", "TheFileYouWant.html", true);
  
  //sending the request
  xhttp.send();
}

Example 6: JS ajax

fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Tags:

Php Example