/ajax/libs/mo/1.1.2/network/ajax.js

https://gitlab.com/Mirros/cdnjs · JavaScript · 135 lines · 109 code · 16 blank · 10 comment · 65 complexity · 312892efcb992a7836df2983a3f67f80 MD5 · raw file

  1. (function(){
  2. /**
  3. * using AMD (Asynchronous Module Definition) API with OzJS
  4. * see http://ozjs.org for details
  5. *
  6. * Copyright (C) 2010-2012, Dexter.Yy, MIT License
  7. * vim: et:ts=4:sw=4:sts=4
  8. */
  9. var browsers = require('../browsers');
  10. var httpParam = function(a) {
  11. var s = [];
  12. if (a.constructor == Array) {
  13. for (var i = 0; i < a.length; i++)
  14. s.push(a[i].name + "=" + encodeURIComponent(a[i].value));
  15. } else {
  16. for (var j in a)
  17. s.push(j + "=" + encodeURIComponent(a[j]));
  18. }
  19. return s.join("&").replace(/%20/g, "+");
  20. };
  21. /**
  22. * From jquery by John Resig
  23. */
  24. var ajax = function(s){
  25. var options = {
  26. type: s.type || "GET",
  27. url: s.url || "",
  28. data: s.data || null,
  29. dataType: s.dataType,
  30. contentType: s.contentType === false? false : (s.contentType || "application/x-www-form-urlencoded"),
  31. username: s.username || null,
  32. password: s.password || null,
  33. timeout: s.timeout || 0,
  34. processData: s.processData === undefined ? true : s.processData,
  35. beforeSend: s.beforeSend || function(){},
  36. complete: s.complete || function(){},
  37. handleError: s.handleError || function(){},
  38. success: s.success || function(){},
  39. accepts: {
  40. xml: "application/xml, text/xml",
  41. html: "text/html",
  42. script: "text/javascript, application/javascript",
  43. json: "application/json, text/javascript",
  44. text: "text/plain",
  45. _default: "*/*"
  46. }
  47. };
  48. if ( options.data && options.processData && typeof options.data != "string" )
  49. options.data = httpParam(options.data);
  50. if ( options.data && options.type.toLowerCase() == "get" ) {
  51. options.url += (options.url.match(/\?/) ? "&" : "?") + options.data;
  52. options.data = null;
  53. }
  54. var status, data, requestDone = false, xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  55. xhr.open( options.type, options.url, true, options.username, options.password );
  56. try {
  57. if ( options.data && options.contentType !== false )
  58. xhr.setRequestHeader("Content-Type", options.contentType);
  59. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  60. xhr.setRequestHeader("Accept", s.dataType && options.accepts[ s.dataType ] ?
  61. options.accepts[ s.dataType ] + ", */*" :
  62. options.accepts._default );
  63. } catch(e){}
  64. if ( options.beforeSend )
  65. options.beforeSend(xhr);
  66. var onreadystatechange = function(isTimeout){
  67. if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) {
  68. requestDone = true;
  69. if (ival) {
  70. clearInterval(ival);
  71. ival = null;
  72. }
  73. status = isTimeout == "timeout" && "timeout" || !httpSuccess( xhr ) && "error" || "success";
  74. if ( status == "success" ) {
  75. try {
  76. data = httpData( xhr, options.dataType );
  77. } catch(e) {
  78. status = "parsererror";
  79. }
  80. options.success( data, status );
  81. } else
  82. options.handleError( xhr, status );
  83. options.complete( xhr, status );
  84. xhr = null;
  85. }
  86. };
  87. var ival = setInterval(onreadystatechange, 13);
  88. if ( options.timeout > 0 )
  89. setTimeout(function(){
  90. if ( xhr ) {
  91. xhr.abort();
  92. if( !requestDone )
  93. onreadystatechange( "timeout" );
  94. }
  95. }, options.timeout);
  96. xhr.send(options.data);
  97. function httpSuccess(r) {
  98. try {
  99. return !r.status && location.protocol == "file:" || ( r.status >= 200 && r.status < 300 ) || r.status == 304 || r.status == 1223 || browsers.safari && !r.status;
  100. } catch(e){}
  101. return false;
  102. }
  103. function httpData(r,type) {
  104. var ct = r.getResponseHeader("content-type");
  105. var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
  106. var data = xml ? r.responseXML : r.responseText;
  107. if ( xml && data.documentElement.tagName == "parsererror" )
  108. throw "parsererror";
  109. if ( type == "script" )
  110. eval.call( window, data );
  111. if ( type == "json" )
  112. data = eval("(" + data + ")");
  113. return data;
  114. }
  115. return xhr;
  116. };
  117. exports.ajax = ajax;
  118. exports.params = httpParam;
  119. })();