ajax封装函数以及调用
*
## get请求
*
function getRequest(url,param,callback,datatype,asynctype) {
if(!datatype){
datatype="json";
}
if(asynctype!=false){
asynctype=true;
}
$.ajax({
type:'get',
url:url,
data:param,
dataType:datatype,
async:asynctype,
success:function(result){
if(result && result.code==200){
addCookie('myinvite_code',result.invitationCode, 188);
addCookie('encryptCode',result.encryptCode, 188);
}
if(typeof(callback) == 'function'){
callback(result);
}else{
}
},error:function(XMLHttpRequest, textStatus, errorThrown){
if(XMLHttpRequest.status == 502){
sessionStorage.setItem('xhrstatus','1');
if(xhrstatus){
window.location.reload();
}else{
sessionStorage.removeItem('xhrstatus');
$.sDialog({
content: '<p>'+'您的网络出错了,请返回到上一个页面重新加载'+" 。</p> ",
okBtn:true,
cancelBtn:false,
okFn: function() { goBack(); }
});
}
}
}
})
}
$('.xxx').click(function(){
var likeparam={}
likeparam.key=key;
likeparam.storeId=result.datas.order_info.store_id;
var requestUrl = ApiUrl+"xxxxxxxxx";
getRequest(requestUrl2,likeparam,function(result) {
if(result.code=="400"){
alert(result.datas.error);
}else{
}
})
function postRequest(url,param,callback,asynctype) {
if(!param) {
param = new Object();
}
if(asynctype!=false){
asynctype=true;
}
$.ajax({
type:'post',
url:url,
data:param,
dataType:'json',
async:asynctype,
success:function(result){
if(result && result.code==200){
addCookie('myinvite_code',result.invitationCode, 188);
addCookie('encryptCode',result.encryptCode, 188);
}else if(result && result.code==400){
errorTipsShow(result.datas.error);
}
if(typeof(callback) == 'function'){
callback(result);
}
},error:function(XMLHttpRequest, textStatus, errorThrown){
if(XMLHttpRequest.status == 502){
sessionStorage.setItem('poststatus','1');
if(xhrstatus){
window.location.reload();
}else{
sessionStorage.removeItem('poststatus');
$.sDialog({
content: '<p>'+'您的网络出错了,请返回到上一个页面重新加载'+" 。</p> ",
okBtn:true,
cancelBtn:false,
okFn: function() { goBack(); }
});
}
}
}
})
}
、
$(document).on('click','.post',function(e){
var _this=$(this);
var shop_cartparam={};
shop_cartparam.xxx = xxx;
var shop_cartfoUrl = ApiUrl+"/CustCart/cart_add";
postRequest(shop_cartfoUrl,shop_cartparam,function(result) {
var rData = result;
if(checkLogin(rData.login)){
if(!rData.datas.error){
alert('正确的会走这里')
}else{
alert('else当然就是错误的啦')
}
}
})
})