解决vue里面“将一个方法的返回值作为另一个方法的参数”的问题

1、首先,我们写两个方法,同样写在method里面,

 query(idcard) {
      let _this = this;
      $.ajax({
        type: "post",
        url: 'http://localhost:8081/jxhh/interfaces/recordController/allResult.do',
        data: {
          //当前用户的身份证
          idCard: '362227*********015'
        },
        dataType: "json",
        //该接口的返回值为当前用户的备案结果数
        success: function (res) {
          for(let item of res.params){
            console.log(item.params);
            _this.items=item.params
            // _this.goResult(JSON.stringify(item.params))  
              //_this.goResult(item.params)
              const res= _this.goResult(item.params) 
              console.log("取到返回值:"+res);
          }

        },
        error: function (result) {
          mui.alert('获取用户备案记录失败');
        }
      });
    },
//查询结果的方法
 goResult(data1) {
    console.log("进入goResult方法")
    let _this=this;
    $.ajax({
        url: 'http://localhost:8080/jxhhhh/interfaces/ydMedical/applyResult.do',
        type: "POST",
        data: {
            "params": data1
        },
        dataType: 'json',
        async: false,
        success: function (res) {
            $("#loading").hide();
            console.log(res);
            if (!res.succ) {
              console.log("进来了"+res.msg);
              _this.msg1=res.msg
              console.log("qudao :"+_this.msg1);
            } else {
  
            }
        },
        error: function (result) {
            console.log(result);
            $("#loading").hide();
            mui.alert('网络开了小差,请稍后再试!');
        }
    });
    return _this.msg1;
}

2、我们直接在我们的一个方法中写return,将得到的结果返回给上一个方法作为入参,这里注意,我们一定要将ajax请求设置为同步,不然会失效!也就是在ajax请求中加上“ async: false”,默认是打开的,也就是异步的,加上这句后就改为同步了!

关于ajax请求异步/同步的详细教程,这里贴个传送门https://www.cnblogs.com/mmzuo-798/p/7098979.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值