Ajax (programação) code example

Example 1: ajax request

$.ajax({
       url : 'more_com.php', //PHP file to execute
       type : 'GET', //method used POST or GET
       data : {variable1 : "some data"}, // Parameters passed to the PHP file
       success : function(result){ // Has to be there !
           
       },

       error : function(result, statut, error){ // Handle errors

       }

    });

// NOTE : Parameters will be available either through $_GET or $_POST according
// to the method you choosed to use. 
// Here you will get your variable "variable1" this way : $_GET['variable1']

Example 2: ajax code

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: url,
    timeout: 5000,
    success: function(data, textStatus ){
       alert('request successful');
    },
    fail: function(xhr, textStatus, errorThrown){
       alert('request failed');
    }
  });

Example 3: what is ajax call

AJAX is allowing the Web page to retrieve small amounts of data from the
server without reloading the entire page. It is used for creating
fast and dynamic web pages.
There are several Wait methods I use to handle
AJAX calls. I add these pieces of code to my AJAX
testing code to handle it.
1. Explicit Waits
webdriver driver = new firefoxdriver();
driver.get("http://somedomain/url_that_delays_loading");
webelement mydynamicelement = (new webdriverwait(driver,10))
.until(expectedconditions.presenceofelementlocated(by.id("myd
ynamicelement")));
2. Implicit Waits
I also go for Implicit Waits, where I can decide on a
certain amount of time require WebDriver to poll the DOM for.  
 In this case your WebDriver will keep looking for an element(s),
 if it had been unavailable immediately. The default
time is set as 0, which can be easily adjusted as you may prefer.
In addition, this set wait time lasts as long as your browser is
open, so the time to search for any element on the page will be
the same.
webdriver driver = new firefoxdriver();
driver.manage().timeouts().implicitlywait(10,
timeunit.seconds);
driver.get("http://somedomain/url_that_delays_loading");
webelement mydynamicelement =
driver.findelement(by.id("mydynamicelement"));

Example 4: ajax programming

// BEGIN
// JavaScript example
// An example of a simple Ajax request using the GET method, written in JavaScript.

// get-ajax-data.js:

// This is the client-side script.
// Initialize the HTTP request.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'send-ajax-data.php');

// Track the state changes of the request.
xhr.onreadystatechange = function () {
	var DONE = 4; // readyState 4 means the request is done.
	var OK = 200; // status 200 is a successful return.
	if (xhr.readyState === DONE) {
		if (xhr.status === OK) {
			console.log(xhr.responseText); // 'This is the output.'
		} else {
			console.log('Error: ' + xhr.status); // An error occurred during the request.
		}
	}
};

// Send the request to send-ajax-data.php
xhr.send(null);
// END

// BEGIN
// send-ajax-data.php:
<?php
// This is the server-side script.
// Set the content type.
header('Content-Type: text/plain');

// Send the data back.
echo "This is the output."; ?>
// END 
  
// BEGIN  
// Fetch is a new native JavaScript API  
fetch('send-ajax-data.php')
    .then(data => console.log(data))
    .catch (error => console.log('Error:' + error));
// END

// BEGIN
// ES7 async/await example:  
async function doAjax() {
    try {
        const res = await fetch('send-ajax-data.php');
        const data = await res.text();
        console.log(data);
    } catch (error) {
        console.log('Error:' + error);
    }
}

doAjax();
// END