Last active
August 29, 2015 14:15
-
-
Save diniremix/8df589843792342197cd to your computer and use it in GitHub Desktop.
pulseJS is a wrapper of javascript functions for Ajax, using JQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pulseJS=(function () { | |
var urlBase='app/pulsePHP/v1/'; | |
getFormsElements=function (form) { | |
var regForm = $('#'+form).serializeArray(); | |
var regFormObject = {}; | |
var cont=0; | |
$.each(regForm,function(i, v) { | |
regFormObject[v.name] = v.value; | |
}); | |
var jsonForm = JSON.stringify(regFormObject,null,2); | |
return jsonForm; | |
}; | |
redirect=function(page){ | |
document.location=page; | |
}; | |
asJson=function (obj){ | |
var jsonForm = JSON.stringify(obj,null,2); | |
return jsonForm; | |
}; | |
isJson=function (cad) { | |
try{ | |
var json = JSON.parse(cad); | |
} | |
catch(e){ | |
return false; | |
} | |
return true; | |
}; | |
ajax=function(url,method,data,dataType,callback){ | |
var myurl, | |
mydata; | |
if(pulseJS.isJson(data)){ | |
myurl=urlBase+url; | |
mydata=data; | |
}else{ | |
myurl=urlBase+url+'/'+data; | |
} | |
//console.log(myurl,method,mydata,dataType); | |
$.ajax({ | |
url: myurl, | |
//method: method ? method : 'GET', | |
type: method ? method : 'GET', | |
data: mydata? {'jsonData':mydata}: {}, | |
dataType: dataType ? dataType : 'json', | |
success: function(data){ | |
if(data.errorCode==0){ | |
(typeof(callback) === 'function') ? callback({errorCode:data.errorCode,response:data.data}) : ''; | |
}else{ | |
(typeof(callback) === 'function') ? callback({errorCode:data.errorCode,message:data.message}) : ''; | |
} | |
}, | |
error:function(data,status){ | |
var response={ | |
status:data.status?data.status:'', | |
statusText:data.statusText?data.statusText:'', | |
responseText:data.responseText?data.responseText:'' | |
}; | |
(typeof(callback) === 'function') ? callback(response,status) : ''; | |
} | |
}); | |
}; | |
return { | |
getFormsElements:getFormsElements, | |
redirect:redirect, | |
asJson:asJson, | |
isJson:isJson, | |
callAjax:ajax | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for visiting, the latest version of pulseJs is now available on Bitbucket
please, visit!