PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/documentation/appframework/$.ajax.md

https://github.com/maoyao/appframework
Markdown | 104 lines | 81 code | 23 blank | 0 comment | 0 complexity | f6bfc0a1f547c14f41978d598bef3234 MD5 | raw file
  1. #$.ajax(options)
  2. ```
  3. Execute an Ajax call with the given options
  4. options.type - Type of request
  5. options.beforeSend - function to execute before sending the request
  6. options.success - success callback
  7. options.error - error callback
  8. options.complete - complete callback - callled with a success or error
  9. options.timeout - timeout to wait for the request
  10. options.url - URL to make request against
  11. options.contentType - HTTP Request Content Type
  12. options.headers - Object of headers to set
  13. options.dataType - Data type of request
  14. options.data - data to pass into request. $.param is called on objects
  15. ```
  16. ##Example
  17. ```
  18. var opts={
  19. type:"GET",
  20. success:function(data){},
  21. url:"mypage.php",
  22. data:{bar:"bar"},
  23. }
  24. $.ajax(opts);
  25. ```
  26. ##Parameters
  27. ```
  28. opts Object
  29. ```
  30. ##Returns
  31. ```
  32. undefined
  33. ```
  34. ##Detail
  35. $.ajax(opts) makes an Ajax call using XMLHttpRequest Object.
  36. Examples below
  37. ```
  38. $.ajax({
  39. type:"GET",
  40. url:"mypage.php",
  41. data:{'foo':'bar'},
  42. success:function(data){}
  43. });
  44. ```
  45. Below is an example for posting a JSON payload to a server
  46. ```
  47. $.ajax({
  48. type:"post",
  49. url:"/api/updateuser/",
  50. data:{id:1,username:'bill'},
  51. contentType:"application/json"
  52. });
  53. ```
  54. When the response has the type 'application/json', we will return the JSON object for you.
  55. When the response has the type "application/xml", we return responseXML.
  56. When the response has the type "text/html", we call $.parseJS on the result to process any JS scripts in the response.
  57. Below is the code to show the dataType values
  58. ```
  59. switch (settings.dataType) {
  60. case "script":
  61. settings.dataType = 'text/javascript, application/javascript';
  62. break;
  63. case "json":
  64. settings.dataType = 'application/json';
  65. break;
  66. case "xml":
  67. settings.dataType = 'application/xml, text/xml';
  68. break;
  69. case "html":
  70. settings.dataType = 'text/html';
  71. break;
  72. case "text":
  73. settings.dataType = 'text/plain';
  74. break;
  75. default:
  76. settings.dataType = "text/html";
  77. break;
  78. case "jsonp":
  79. return $.jsonP(opts);
  80. }
  81. ```