/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
- (function(){
- /**
- * using AMD (Asynchronous Module Definition) API with OzJS
- * see http://ozjs.org for details
- *
- * Copyright (C) 2010-2012, Dexter.Yy, MIT License
- * vim: et:ts=4:sw=4:sts=4
- */
- var browsers = require('../browsers');
- var httpParam = function(a) {
- var s = [];
- if (a.constructor == Array) {
- for (var i = 0; i < a.length; i++)
- s.push(a[i].name + "=" + encodeURIComponent(a[i].value));
- } else {
- for (var j in a)
- s.push(j + "=" + encodeURIComponent(a[j]));
- }
- return s.join("&").replace(/%20/g, "+");
- };
- /**
- * From jquery by John Resig
- */
- var ajax = function(s){
- var options = {
- type: s.type || "GET",
- url: s.url || "",
- data: s.data || null,
- dataType: s.dataType,
- contentType: s.contentType === false? false : (s.contentType || "application/x-www-form-urlencoded"),
- username: s.username || null,
- password: s.password || null,
- timeout: s.timeout || 0,
- processData: s.processData === undefined ? true : s.processData,
- beforeSend: s.beforeSend || function(){},
- complete: s.complete || function(){},
- handleError: s.handleError || function(){},
- success: s.success || function(){},
- accepts: {
- xml: "application/xml, text/xml",
- html: "text/html",
- script: "text/javascript, application/javascript",
- json: "application/json, text/javascript",
- text: "text/plain",
- _default: "*/*"
- }
- };
-
- if ( options.data && options.processData && typeof options.data != "string" )
- options.data = httpParam(options.data);
- if ( options.data && options.type.toLowerCase() == "get" ) {
- options.url += (options.url.match(/\?/) ? "&" : "?") + options.data;
- options.data = null;
- }
-
- var status, data, requestDone = false, xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
- xhr.open( options.type, options.url, true, options.username, options.password );
- try {
- if ( options.data && options.contentType !== false )
- xhr.setRequestHeader("Content-Type", options.contentType);
- xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
- xhr.setRequestHeader("Accept", s.dataType && options.accepts[ s.dataType ] ?
- options.accepts[ s.dataType ] + ", */*" :
- options.accepts._default );
- } catch(e){}
-
- if ( options.beforeSend )
- options.beforeSend(xhr);
-
- var onreadystatechange = function(isTimeout){
- if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) {
- requestDone = true;
- if (ival) {
- clearInterval(ival);
- ival = null;
- }
- status = isTimeout == "timeout" && "timeout" || !httpSuccess( xhr ) && "error" || "success";
- if ( status == "success" ) {
- try {
- data = httpData( xhr, options.dataType );
- } catch(e) {
- status = "parsererror";
- }
-
- options.success( data, status );
- } else
- options.handleError( xhr, status );
- options.complete( xhr, status );
- xhr = null;
- }
- };
- var ival = setInterval(onreadystatechange, 13);
- if ( options.timeout > 0 )
- setTimeout(function(){
- if ( xhr ) {
- xhr.abort();
- if( !requestDone )
- onreadystatechange( "timeout" );
- }
- }, options.timeout);
-
- xhr.send(options.data);
- function httpSuccess(r) {
- try {
- return !r.status && location.protocol == "file:" || ( r.status >= 200 && r.status < 300 ) || r.status == 304 || r.status == 1223 || browsers.safari && !r.status;
- } catch(e){}
- return false;
- }
- function httpData(r,type) {
- var ct = r.getResponseHeader("content-type");
- var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0;
- var data = xml ? r.responseXML : r.responseText;
- if ( xml && data.documentElement.tagName == "parsererror" )
- throw "parsererror";
- if ( type == "script" )
- eval.call( window, data );
- if ( type == "json" )
- data = eval("(" + data + ")");
- return data;
- }
- return xhr;
- };
- exports.ajax = ajax;
- exports.params = httpParam;
- })();