附上源代码:
Sigma.Ajax = Sigma.$class( {
properties : function(){return {
method: 'post',
jsonParamName : Sigma.AjaxDefault.paramName,
async: true,
urlEncoded: true,
encoding: null, //'GBK',
mimeType: null, //'text/html',
beforeSend: Sigma.$empty,
onComplete: Sigma.$empty,
onSuccess: Sigma.$empty,
onFailure: Sigma.$empty,
onCancel: Sigma.$empty,
xhr : '',
url: '',
data: '',
paramType : 'jsonString', // jsonString queryString xml
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Accept': 'text/javascript, text/html, application/xml,application/json, text/xml, */*'
},
autoCancel: false,
evalScripts: false,
evalResponse: false,
responseContentType :'',
dataUrl : false,
queryParameters : null
};},
setQueryParameters : function(queryParameters){
this.queryParameters=queryParameters;
},
initialize: function(options){
options= options || {};
if (Sigma.$type(options,'string')){
options={ url : options };
}
if (!(this.xhr = this.getXHR())) { return; }
var _header=Sigma.$extend(this.headers,options.headers);
Sigma.$extend(this,options);
if (this.mimeType){
_header['X-Response-MimeType']=this.mimeType;
}
this.headers = _header;
},
send: function(options){
this.running = true;
if (Sigma.$type(options,'string')){
options={ data : options };
}
options = Sigma.$extend({data: this.data, url: this.url, method: this.method}, options);
var data = options.data, url = options.url, method = String(options.method).toLowerCase();
if (Sigma.$invoke(this,'beforeSend',[this.xhr,data])===false){
return this;
}
if (this.urlEncoded && method == 'post'){
var encoding = (this.encoding) ? '; charset=' + this.encoding : '';
this.setHeader('Content-type','application/x-www-form-urlencoded' + encoding);
}
switch(Sigma.$type(data)){
case 'object':
if (this.paramType =='jsonString') {
var _data=Sigma.$json(data);
data = { };
data[this.jsonParamName]=_data;
}
data = Sigma.toQueryString(data);
break;
default:
// do nothing;
}
var _queryParameters;
if (this.queryParameters && Sigma.$type(this.queryParameters,'object')){
_queryParameters = Sigma.toQueryString(this.queryParameters);
}else if (Sigma.$type(this.queryParameters,'string')){
_queryParameters = this.queryParameters;
}
if (_queryParameters && Sigma.$type(data,'string')){
data = data + '&'+ _queryParameters;
}
//alert(_queryParameters)
if ( method == 'post'){
// todo fixed url too long. err code : 122
var idx= url.indexOf('?');
if (idx>=0){
data=url.substring(idx+1)+'&'+data;
url=url.substring(0,idx);
}
}else if (data && ( method == 'get' || this.dataUrl) ){
url = url + (url.indexOf('?')>=0 ? '&' : '?') + data;
data = null;
}
var _ajax=this;
this.xhr.open(method.toUpperCase(), url, this.async);
this.xhr.onreadystatechange = function(){
return _ajax.onStateChange.apply(_ajax,arguments);
};
for (var key in this.headers ){
try{
this.xhr.setRequestHeader(key, this.headers[key]);
}catch(e){
}
}
//alert(data);
this.xhr.send(data);
if (!this.async) { this.onStateChange();}
return this;
},
onStateChange: function(){
if (this.xhr.readyState != 4 || !this.running) { return; }
this.running = false;
this.status = 0;
try {
this.status = this.xhr.status;
}catch (e){
}
this.onComplete();
if (this.isSuccess()){
this._onSuccess();
}else{
this._onFailure();
}
this.xhr.onreadystatechange = Sigma.$empty;
},
isScript: function(){
return (/(ecma|java)script/).test(this.getHeader('Content-type'));
},
isSuccess: function(){
var status=this.xhr.status;
return ((status >= 200) && (status < 300));
},
_onSuccess: function(){
this.response = {
'text': this.xhr.responseText,
'xml': this.xhr.responseXML
};
this.onSuccess(this.response);
},
_onFailure: function(e){
this.onFailure(this.xhr,e);
},
setHeader: function(name, value){
this.headers[name]= value;
return this;
},
getHeader: function(name){
try{
return this.xhr.getResponseHeader(name);
}catch (e){
return null;
}
},
getXHR: function(){
return (window.XMLHttpRequest) ? new XMLHttpRequest() : ((window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : false);
},
cancel: function(){
if (!this.running) { return this; }
this.running = false;
this.xhr.abort();
this.xhr.onreadystatechange = Sigma.$empty;
this.xhr = this.getXHR();
this.onCancel();
return this;
}
} );
结果用到的地方提示,exception: Sigma.Ajax is not a constructor.
【 在 JavaExpert 的大作中提到: 】
: 看到了一段javascript源代码中用到了$class,这个$class是指什么?谢谢!
--
FROM 140.206.244.*