ajax拼接外部变量,封装的ajax请求结果如何赋值给外部变量?

navSide.js

我想将_home.categoryLevel1请求中的res.allCategory赋值给option对象中的sideList,可是在外面没法读取到post请求中的结果。请问如何赋值成功。

var navSide = {

option:{

id:'',

sideList:[]

},

init: function () {

this.onLoad();

},

onLoad:function(){

var _this=this;

_home.categoryLevel1(function(res){

_this.option.sideList=res.allCategory;

console.log(_this.option.sideList);//['a','b','c','d'],这里能拿到res里面的数组

});

console.log(categoryL1);//undefined,此处无法拿到请求中的值,因此赋值无效

}

};

module.exports = navSide;

service.js

将ajax的异步请求改成了同步请求也无效

var _home={

categoryLevel1 : function(resolve, reject){

_mm.request({

url : _mm.getServerUrl('/home/category'),

method : 'POST',

async : false,

success : resolve,

error : reject

});

},

};

module.exports=_home;

mm.js封装的ajax公共方法

var _mm={

request:function(param){

$.ajax({

type:param.method||'post',

url:param.url||'',

dataType:param.type||'json',

async : param.async||true,

data:JSON.stringify(param.data)||'',

contentType: "application/json; charset=utf-8",

success:function(res){

//请求成功的回调

if(res.resultCode==="200"){

typeof param.success==='function'&&param.success(res.data,res.message);

}

},

error:function(err){

//请求失败的执行

}

})

},

}

module.exports=_mm;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值