The ajax() method is used to perform an AJAX (asynchronous HTTP) request.
All jQuery AJAX functions: load(), get(), post() use the ajax() method. This is the most powerful and customizable Ajax method, but its syntax is more complex than the others.
$.ajax({ name:value, name:value, ...})- The parameters specify one ore more name:value pairs that configure the Ajax request. They are presented in a list below, but first here's a simple POST request:
$.ajax({ type: 'POST', url: 'script.php', data: 'name=some_name&id=an_id', success: function(response){ $('#result').html(response); } });The data "name=some_name&id=an_id" are send to the "script.php" file through POST method, then the response from the server is placed into an HTML element which has id="result".
$.ajax({ url: "file.php", async: false });
$.ajax({
url: "file.php",
beforeSend: function() {
xhr.setRequestHeader("Accept", "text/javascript");
$('#resp').html('Loading...');
},
success: function(response){
$('#resp').html('response');
}
});
$.ajax({ url: "file.html", cache: false });
$.ajax({ url: "file.php", complete: function() { alert('complete'); } });
$.ajax({ type: "POST", url: "file.php", data: "{'name1':'value', 'name2':'value'}", contentType: "application/json; charset=utf-8", dataType: "json" });
$.ajax({ url: "file.php", context: $('#idd'), success: function(){ $(this).html('Done'); // this will be "#idd" } });
$.ajax({ type: "GET", url: "file.php", data: "id=78&name=some_name" });
$.ajax({ url: "file.php", data: {'json':'datas'}, contentType: "application/json; charset=utf-8", dataFilter: function(data) { var resp = eval('(' + data + ')'); return resp; }, success: function(response, status, xhr){ $('#idd').html(response.property); } });
$.ajax({ // Load and execute a JavaScript file type: "GET", url: "file.js", dataType: "script" });
$.ajax({ url: "file.php", error: function(xhr, status, error) { alert(error); } });
$.ajax({ url: "file.php", global: false, success: function(msg){ alert(msg); } });
$.ajax({ headers: { "Content-Type": "application/json", "Accept": "application/json" }, type: "POST", url: "file.php", data: {'json': 'data'}, dataType: "json", success: function(json){ // do something... } });
$.ajax({ url: "file.php", ifModified: true, success: function(data, status){ if(status!="notmodified") { $("#display").append(data); } } });
// jQuery will change the url to include &jsonp=jsonpmethod $.ajax({ dataType: 'jsnp', data: 'id=10', jsonp: 'jsnp', url: "file.php", });
$.ajax({ url: "file.php", username: 'name', password: 'pass', success: function(response){ $("#display").html(response); } });
// Sends an xml document as data to the server var xmlDocument = [create xml document]; $.ajax({ url: "page.php", processData: false, data: xmlDocument, success: function(response){ $("#display").html(response); } });
$.ajax({ type: "GET", url: "file.php", scriptCharset: "utf-8", contentType: "application/x-www-form-urlencoded; charset=UTF-8", data: "id=12&name=some_name", success: function(response){ $("#idd").html(response); } });
// alert when the response status is a 404 $.ajax({ url: "file.php", statusCode: { 404: function() { alert('page not found'); } } });
$.ajax({ url: "file.php", data: 'id=an_id', success: function(response, status, xhr){ if(status=="success") { $("#display").html(response); } else { alert(status+ ' - '+ xhr.status); } } });
// allow 3000 millisecond timeout for the AJAX request $.ajax({ url: "file.php", timeout: 3000, success: function(){ $("#response").text('Success'); } });
$.ajax({ type: "POST", url: "file.php", data: 'name=some_name&pass=pasword', success: function(response){ $("#idd").text(response); } });
$.ajax({ url: "file.php", success: function(response){ $("#idd").text(response); } });
<?php // define an array with address for diferent courses $crs = array( 'other'=>'', 'php-mysql'=>'php-mysql', 'javascript'=>'javascript', 'actionscript'=>'actionscript/lessons-as3', 'jquery'=>'jquery/jquery-tutorials' ); // check if there are data sent through POST, with keys "nm", "cs", and "cmt" if(isset($_POST['nm']) && isset($_POST['cs']) && isset($_POST['cmt'])) { $_POST = array_map("strip_tags", $_POST); // removes posible tags from POST // get data $nm = $_POST['nm']; $cs = $_POST['cs']; $cmt = $_POST['cmt']; // define a variable with the response of this script $rehtml = '<h4>Hy '. $nm. '</h4> Here`s the link for <b>'. $cs. '</b> Course: <a href="https://coursesweb.net/'. $crs[$cs]. '">'. $cs. '</a><br />Your comments: <i>'. $cmt. '</i>'; } else $rehtml = 'Invalid data'; echo $rehtml; // output (return) the response ?>
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jQuery Ajax - PHP</title> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // when the form "#crs" is submited $('#crs').submit(function() { // get form data var nm = $('#crs #nm').val(); var cs = $('#crs #cs').val(); var cmt = $('#crs #cmt').val(); // put form data in a JSON format that will be sent to the server var data_json = {'nm':nm, 'cs':cs, 'cmt':cmt}; // sets the ajax() method to send data through POST to a PHP script // if occurs an error, alerts the request status (xhr.status) and the error // when the request succeeds, place the response in a HTML tag with id="resp" $.ajax({ type: 'post', url: 'script.php', data: data_json, beforeSend: function() { // before send the request, displays a "Loading..." messaj in the element where the server response will be placed $('#resp').html('Loading...'); }, timeout: 10000, // sets timeout for the request (10 seconds) error: function(xhr, status, error) { alert('Error: '+ xhr.status+ ' - '+ error); }, success: function(response) { $('#resp').html(response); } }); return false; // required to not open the page when form is submited }); }); --></script> </head> <body> <div id="resp"></div> <h4>Fill the form:</h4> <form action="script.php" method="post" id="crs"> Name: <input type="text" name="nm" id="nm" /><br /> Course: <select name="cs" id="cs"> <option value="other">Other</option> <option value="php-mysql">PHP-MySQL</option> <option value="javascript">JavaScript</option> <option value="actionscript">ActionScript</option> <option value="jquery">jQuery</option> </select><br /> Comments:<br /> <textarea name="cmt" id="cmt" cols="20" rows="3"></textarea> <input type="submit" value="submit" /> </form> </body> </html>Demo:
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net