1 $.ajax({
2 url : 'more_com.php', //PHP file to execute
3 type : 'GET', //method used POST or GET
4 data : {variable1 : "some data"}, // Parameters passed to the PHP file
5 success : function(result){ // Has to be there !
6
7 },
8
9 error : function(result, statut, error){ // Handle errors
10
11 }
12
13 });
14
15// NOTE : Parameters will be available either through $_GET or $_POST according
16// to the method you choosed to use.
17// Here you will get your variable "variable1" this way : $_GET['variable1']
1 $.ajax({
2 url: "Url",
3 dataType: "json",
4 type: "Post",
5 async: true,
6 data: {"Key":value,"Key2":value2},
7 success: function (data) {
8
9 },
10 error: function (xhr, exception, thrownError) {
11 var msg = "";
12 if (xhr.status === 0) {
13 msg = "Not connect.\n Verify Network.";
14 } else if (xhr.status == 404) {
15 msg = "Requested page not found. [404]";
16 } else if (xhr.status == 500) {
17 msg = "Internal Server Error [500].";
18 } else if (exception === "parsererror") {
19 msg = "Requested JSON parse failed.";
20 } else if (exception === "timeout") {
21 msg = "Time out error.";
22 } else if (exception === "abort") {
23 msg = "Ajax request aborted.";
24 } else {
25 msg = "Error:" + xhr.status + " " + xhr.responseText;
26 }
27 if (callbackError) {
28 callbackError(msg);
29 }
30
31 }
32 });
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>
1AJAX is allowing the Web page to retrieve small amounts of data from the
2server without reloading the entire page. It is used for creating
3fast and dynamic web pages.
4There are several Wait methods I use to handle
5AJAX calls. I add these pieces of code to my AJAX
6testing code to handle it.
71. Explicit Waits
8webdriver driver = new firefoxdriver();
9driver.get("http://somedomain/url_that_delays_loading");
10webelement mydynamicelement = (new webdriverwait(driver,10))
11.until(expectedconditions.presenceofelementlocated(by.id("myd
12ynamicelement")));
132. Implicit Waits
14I also go for Implicit Waits, where I can decide on a
15certain amount of time require WebDriver to poll the DOM for.
16 In this case your WebDriver will keep looking for an element(s),
17 if it had been unavailable immediately. The default
18time is set as 0, which can be easily adjusted as you may prefer.
19In addition, this set wait time lasts as long as your browser is
20open, so the time to search for any element on the page will be
21the same.
22webdriver driver = new firefoxdriver();
23driver.manage().timeouts().implicitlywait(10,
24timeunit.seconds);
25driver.get("http://somedomain/url_that_delays_loading");
26webelement mydynamicelement =
27driver.findelement(by.id("mydynamicelement"));