var $http = function(url,config){
this.url = url;
this.error = function(xhr){console.log(xhr.responseText);};
this.success = function(ret){console.log(ret);};
this.dataType = "json";
this.setting = $.extend({
url:this.url,
success:this.success,
error:this.error,
dataType:this.dataType
},config||{});
};
$http.prototype = {
send:function(callback){
if(callback && typeof callback=='function')
this.success = callback;
$.ajax(this.setting);
}
}
this.url = url;
this.error = function(xhr){console.log(xhr.responseText);};
this.success = function(ret){console.log(ret);};
this.dataType = "json";
this.setting = $.extend({
url:this.url,
success:this.success,
error:this.error,
dataType:this.dataType
},config||{});
};
$http.prototype = {
send:function(callback){
if(callback && typeof callback=='function')
this.success = callback;
$.ajax(this.setting);
}
}
new $http('/springmvc/my/upload').send();
----------------------------------------------------------------------每次new的时候创建局部变量,不好,局部变量应该放到prototype中,如下:
var $http = function(url,config){
this.setting = $.extend(
{
url:url,
success:function(ret){console.log(ret);},
error:function(xhr){console.log(xhr.responseText);},
dataType:this.dataType,
type:'GET'
}, config || {}
);
};
$http.prototype = {
send:function(callback){
if(callback && typeof callback=='function')
this.setting.success = callback;
$.ajax(this.setting);
}
}
new $http('/shiro/query').send(function(ret){
alert(ret.success);
});