Typical Ajax Call
$.ajax({ // $.post, $.get and $.getJson are preferred url: "demo_test.txt", type: "GET or POST", contentType: "application/x-www-form-urlencoded", contentType: "application/json" complete(xhr,status), error(xhr,status,error), success: function(result,status,xhr){ $("#div1").html(result); } });
|
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { // readyState 4 = request finished. response is ready if (this.readyState == 4 && this.status == 200) { // Do Stuff } }; xhttp.open("GET|POST", "url", true); // var data = JSON.stringify({"email": "hey@mail.com", "password": "101010"}); xhttp.send(data^ or "Query Params");
|
JQuery AJAX GET POST getJSON Shorthand
var request = $.get( // Also $.post or $.getJSON "example.php", { time: "2pm", color: "red", "choices[]": ["Jon", "Susan"] }, function(data, textStatus, jqXHR) { alert( "success" ); }).done(function(data, textStatus, jqXHR) { alert( "second success" ); }).fail(function(data, textStatus) { alert( "error" ); }).always(function(data, textStatus, jqXHR) { alert( "finished" ); });
|
$.post form data can be serialized with: $( "#myform" ).serialize()
QueryString params for $.get can be generated (from a hash) with $.param( myHash )
Loading Scripts and Remote Page Fragments
$("#myDiv").load("demo_test.txt"); |
loads the content of the file "demo_test.txt" into #myDiv element |
$("#myDiv").load("demo_test.txt #p1"); |
Parses DOM from demo_test.txt for #p1 and loads content of #p1 into #myDiv |
$.getScript("demo_ajax_script.js"); |
Load a JavaScript file from the server using a GET HTTP request, then execute it. |
JQuery Low Level Interface
$.ajaxPrefilter() |
Handle custom Ajax options or modify existing options before each request is sent and before they are processed by $.ajax() |
jQuery.ajaxSetup() |
Set global default values for future Ajax requests. Its use is not recommended. |
jQuery.ajaxTransport() |
Creates an object that handles the actual transmission of Ajax data |
|