Hola estoy haciendo un plugin jquery sobre ajax level2 con reportes de errores mediante try catch mi inconveniente es el siguiente, en el plugin puedo elegir si mis datos van a ser normales o de forma json, el problema surge al obtener el error de su conversion posteo aquí el codigo y la linea en la que no puedo lograr que imprima o devuelva bien su exception
Plugin
Código Javascript
:
Ver original(function($){
// Constructor
$.AjaxPrepare = function(option){
// Definiendo opciones
var options = $.extend($.AjaxPrepare.default,option);
try{
var ajax,
dataPost,
data = new Array();
// Init
$.AjaxPrepare.init();
// Parseo
$.AjaxPrepare.parse(options.method,options.insert);
// Before
$.AjaxPrepare.before(options.onBefore);
// Open
$.AjaxPrepare.open(options.method,options.url);
// Estados
$.AjaxPrepare.status(options.type,options.onLoad,options.onLoaded);
// Progreso
$.AjaxPrepare.progress(options.onProgress);
// Envio final
$.AjaxPrepare.send(options.method);
}catch(e){
if(typeof options.onError == 'function')
options.onError(e);
else
console.log(e);
}
}
// Inicializador
$.AjaxPrepare.init = function(){
// Ajax
if(window.XMLHttpRequest)
ajax = new XMLHttpRequest();
else if(window.ActiveXObject)
ajax = new ActiveXObject("Microsoft.XMLHTTP");
else
throw new Exception('[AjaxPrepare]','El objeto ajax no pudo ser inicializado.');
// Formdata
if(window.FormData)
dataPost = new FormData();
else
throw new Exception('[AjaxPrepare]','El objeto formdata no pudo ser inicializado, no se podrán enviar archivos.');
}
// Parseo
$.AjaxPrepare.parse = function(method,datos){
// POST
if(method=='post'){
for(var i = 0; i < datos.length; i++){
dataPost.append(datos[i][0], datos[i][1]);
}
}else{
data = new String('?');
for(var i=0; i < datos.length; i++){
if(i<datos.length)
data +=datos[i][0]+'='+datos[i][1]+'&';
else
data +=datos[i][0]+'='+datos[i][1];
}
}
}
// Before
$.AjaxPrepare.before = function(method){
// Funcion previa
if(typeof method == 'function')
method();
}
// Open
$.AjaxPrepare.open = function(method,url){
if(method=='post')
ajax.open(method,url,true);
else
ajax.open(method,url+data,true)
}
// Status
$.AjaxPrepare.status = function(type,onLoad,onLoaded){
var report = 0;
// Estados
ajax.onreadystatechange = function(){
if(ajax.readyState==1 || ajax.readyState==2 || ajax.readyState==3){
onLoad();
}
if(ajax.readyState==4 && ajax.status==200){
// Si debe ser json
if(type=='json'){
// Si retorna un objeto
if(typeof $.AjaxPrepare.JSON(ajax.response) == 'object'){
onLoaded($.AjaxPrepare.JSON(ajax.response));
// Si retorna una excepcion
}else{
throw new Exception("[AjaxPrepare]","No se pudieron obtener los datos correctamente. [JSON]");
}
}else{
onLoaded(ajax.response);
}
}
if(ajax.readyState==4 && ajax.status==404){
throw new Error('[AjaxPrepare]','No se pudo conectar a la pagina solicitada.');
}
};
}
// Json
$.AjaxPrepare.JSON = function(response){
try{
return JSON.parse(response);
}catch(e){
return false;
}
}
// Progreso de peticion
$.AjaxPrepare.progress = function(onProgress){
ajax.upload.onprogress = function(e){
if(e.lengthComputable){
var porcentaje=Math.ceil((e.loaded/e.total)*100);
if(typeof onProgress=='function')
onProgress(porcentaje);
}
};
}
// Send
$.AjaxPrepare.send = function(method){
if(method=='post')
ajax.send(dataPost);
else
ajax.send(null);
}
// Exception
Exception = function(name,message){
return {
name: name,
message: message
}
}
// Opciones por default
$.AjaxPrepare.default = {
insert : [], // [ ['name',value],['name','value'] ] name: nombre con el que identificas desde php. value: valor que obtendras en php [string | int | file].
url : String, // Url php, etc... procesador de informacion. [String].
method : String, // Metodo a emplear para la peticion ['POST' | 'GET'].
type : String, // Type json, o normal
onBefore : function(){}, // onBefore(){} Funcion antes del envio.
onLoad : function(){}, // onLoad(){} Funcion equivalente a readyState[1,2,3].
onLoaded : function(response){}, // onLoaded(){} Funcion equivalente a readyState[4] y Status[200].
onProgress : function(progress){}, // onProgress(){} Funcion que devuelve el procentaje de avance de la peticion.
onError : function(){}, // onError(){} Equivalente a readyState[4] y Status[404]
}
})(jQuery);
Forma de uso
Código Javascript
:
Ver original$.AjaxPrepare({
insert : [['obtain','clientes'],['cantidad',40],['page',page]],
url : BASE_URL+'fillClient/',
method : 'post',
type : 'json',
onBefore : function(){
// Reportamos el progreso
$('#listClientes layout.box-message[data-report-progress]').css('display','block');
},
//onLoad : function(){},
onLoaded : function(response){
// Eliminamos el reporte de progreso
$('#listClientes layout.box-message[data-report-progress]').css('display','none');
// Obtenemos los datos y los imprimimos
structure(response);
},
onProgress : function(progress){},
onError : function(e){
// Reporte de exceptions
controlExeptions(e); //------------
},
});
[ERROR]: el error que me devuelve es #object uncauht

alguien que me ayude por favor ya hace dias que le estoy dando vueltas a este inconveniente, seria muy util!
LINEA DEL PROBLEMA 143
[Aclaracion] Funciona todo el plugin solo no recibo esa exception