MENU
Generic $.ajax(…)
A typical, low-level, asynchronous JQuery Ajax request has the form:
$.ajax([ String u rl] [, Object settings ] ) [.done(function( data , status, xhr) f1 )] [.fail(function(xhr, status, error) f2 )] [.always(function( data |xhr, status, xhr|error) f3 )] [.then(function( data , status, xhr) f4 , function(xhr, status, error) f5 )]
'url' is a string that specifies the URL to which the request is sent. 'settings' contains a set, enclosed within {}, of optional key:value pairs.$.ajax().done() specifies the function to call when the request succeeds. $.ajax().fail() specifies the function to call when the request fails. $ajax().always() specifies the function to call when the request completes, regardless of whether it succeeds or fails. $ajax(). then() combines $ajax().done() and $ajax().fail().'response' is a string containing the response text. 'status' is a string which can be “success”, “notmodified”, “error”, “timeout”, “abort”, “parsererror”. 'xhr' contains the XMLHttpRequest object. 'error' is a string containing the error message.
Basic Settings |
url: string contains the URL to which the request is sent. |
type: “GET” / ”POST” sets the type of request. |
data: plainObject/string contains the data to be sent to the server. The value is a set of key:value pairs, or a query string. |
async: true/false specifies whether the request is asynchronous. When set to false, the execution is blocked until a response is received. |
cache: true/false specifies whether the request is to be cached. |
global: true/false specifies whether to trigger global AJAX event handlers for this request. |
timeout: number sets a timeout (in milliseconds) for the request. |
statusCode: plainObject sets the function to be called when the response has the corresponding code. |
RESETRUNFULL
<!DOCTYPE html><html><head>
<script src="jquery-3.5.1.min.js"></script><script>$(document).ready(function(){
$("button").click(function(){
num=document.getElementsByTagName("input")[0].value;
$.ajax("sum.php",
{type: "POST",
data: {num:num},
statusCode:{200:alert_success,
404:alert_page_not_found}
})
.done(function(msg){
$("p:first").html(msg);
})
.always(function(msg,status,xhr){
$("p:last").html("Status:"+status+"("+xhr.status+")");
});
});});function alert_success(){
alert("SUCCESS!");}function alert_page_not_found(){
alert("PAGE NOT FOUND!");}</script></head><body>Enter the number to add:<input type="text" size="5" value="10"><br/><button type="button" id="button">Get the sum</button><p></p><p></p></body></html>
<?php$num = $_POST['num'];$sum=file("sum.txt")[0];echo $num+$sum;file_put_contents("sum.txt",$sum+$num);?>
Advanced Settings |
accepts: plainObject |
beforeSend: function(xhr,settings) |
contents: plainObject |
contentType: string |
context: plainObject |
converters: plainObject |
crossDomain: true/false |
dataFilter: function(data,type) |
dataType: “xml”, “html”, “script”, “json”, “jsonp”, “text” |
headers: plainObject |
ifModified: true/false |
isLocal: true/false |
jsonp: string |
jsonpCallback: string/function() |
mimeType: string |
password: string |
processData: true/false |
scriptCharset: string |
traditional: true/false |
username: string |
xhr: function() |
xhrFields: plainObject |
$.ajaxPrefilter( [String dataTypes,] function( Object opt, Object originalOpt, jqXHR xhr) handler ) handle custom Ajax options or modify existing options before each request is sent and before they are processed by $.ajax().$.ajaxTransport(String dataType, function( Object opt, Object originalOpt, jqXHR xhr) handler) create an object that handles the actual transmission of Ajax data.