/documentation/appframework/$.ajax.md
Markdown | 104 lines | 81 code | 23 blank | 0 comment | 0 complexity | f6bfc0a1f547c14f41978d598bef3234 MD5 | raw file
- #$.ajax(options)
- ```
- Execute an Ajax call with the given options
- options.type - Type of request
- options.beforeSend - function to execute before sending the request
- options.success - success callback
- options.error - error callback
- options.complete - complete callback - callled with a success or error
- options.timeout - timeout to wait for the request
- options.url - URL to make request against
- options.contentType - HTTP Request Content Type
- options.headers - Object of headers to set
- options.dataType - Data type of request
- options.data - data to pass into request. $.param is called on objects
-
- ```
- ##Example
- ```
- var opts={
- type:"GET",
- success:function(data){},
- url:"mypage.php",
- data:{bar:"bar"},
- }
- $.ajax(opts);
-
- ```
- ##Parameters
- ```
- opts Object
- ```
- ##Returns
- ```
- undefined
- ```
- ##Detail
- $.ajax(opts) makes an Ajax call using XMLHttpRequest Object.
- Examples below
- ```
- $.ajax({
- type:"GET",
- url:"mypage.php",
- data:{'foo':'bar'},
- success:function(data){}
- });
- ```
- Below is an example for posting a JSON payload to a server
- ```
- $.ajax({
- type:"post",
- url:"/api/updateuser/",
- data:{id:1,username:'bill'},
- contentType:"application/json"
- });
- ```
- When the response has the type 'application/json', we will return the JSON object for you.
- When the response has the type "application/xml", we return responseXML.
- When the response has the type "text/html", we call $.parseJS on the result to process any JS scripts in the response.
- Below is the code to show the dataType values
- ```
- switch (settings.dataType) {
- case "script":
- settings.dataType = 'text/javascript, application/javascript';
- break;
- case "json":
- settings.dataType = 'application/json';
- break;
- case "xml":
- settings.dataType = 'application/xml, text/xml';
- break;
- case "html":
- settings.dataType = 'text/html';
- break;
- case "text":
- settings.dataType = 'text/plain';
- break;
- default:
- settings.dataType = "text/html";
- break;
- case "jsonp":
- return $.jsonP(opts);
- }
- ```