zoukankan      html  css  js  c++  java
  • Ajax中遇到的一点细节问题

    async: false, 这个与原来可以解决同步问题。

    同时,Ajax中返回bool值时都是不成功的。

    1. function  refundUseCoupon(trxOrderGoodsId){  
    2.     $.ajax({  
    3.         type:'GET',  
    4.         url:'<%=contextPath%>/ucenter/getCouponPayment.do',  
    5.         data:{"trxGoodsId":trxOrderGoodsId},  
    6.         dataType:'json',  
    7.         cache : false,  
    8.         async:false,  
    9. <pre name="code" class="javascript">        success:function(data){  
    10.             if((data.couponId != null) && (data.couponId !='') && (data.couponId>0) ){  
    11.                 return true;  
    12.             }else{  
    13.             return  false;  
    14.             }  
    15.         },  
    16.         error : function(error) {  
    17.             return false;  
    18.         }  
    19.     });  
    20.     return result;  
    21. }</pre>  
    22. <pre></pre>  
    23. <br>  
    24. 上面的函数,很简单,只作为提示用,想返回truefalse,很简单,就是没注意。  
    25. <p></p>  
    26. <p>结果测试.alert:undefined</p>  
    27. <p>下面是调用时的测试代码</p>  
    28. <p></p>  
    29. <pre name="code" class="javascript">// 调用测试  
    30. funaction refund (){  
    31.     var refflag=false;  
    32.     refflag=refundUseCoupon(_id);  
    33.     alert(refflag);  
    34.     if(refflag){  
    35.         //  
    36.     }else{  
    37.         //  
    38.     }  
    39.   
    40. }</pre><br>  
    41. 在上述的代码中已经return了。结果就是不返回。自己想了下。感觉在ajax中返回实际相当于没有返回值。
      1. <p></p>  
      2. <p>类似:</p>  
      3. <pre name="code" class="javascript">function refundUseCoupon(){  
      4.  aa();//ajax...aa方法相当于是ajax方法。所以怎么返回也是无效的。本方法没有返回。  
      5.   ......  
      6. //应该在ajax外面执行return操作  
      7. return true;  
      1. <p>下面的写法就可以了。</p>  
      2. <p></p>  
      3. <pre name="code" class="javascript">function  refundUseCoupon(trxOrderGoodsId){  
      4.     var result=false;  
      5.     $.ajax({  
      6.         type:'GET',  
      7.         url:'<%=contextPath%>/ucenter/getCouponPayment.do',  
      8.         data:{"trxGoodsId":trxOrderGoodsId},  
      9.         dataType:'json',  
      10.         cache : false,  
      11.         async : false,  
      12.         success:function(data){  
      13.             if((data.couponId != null) && (data.couponId !='') && (data.couponId>0) ){  
      14.                 result= true;  
      15.             }else{  
      16.                 result= false;  
      17.             }  
      18.         },  
      19.         error : function(error) {  
      20.             result= false;  
      21.         }  
      22.     });  
      23.     return result;//在ajax之外执行return  
      24. }</pre><br>  
      25. 注意:async:false。设置为同步的,否则未等ajax执行完,就直接return初始的var resutt=false了。  

    在不加async: false,时

  • 相关阅读:
    DHCP分配ip地址。0.0.0.0与255.255.255.255
    net-snmp配置文件详解
    net-snmp开发中出现“Error opening specified endpoint"" ”的解决方案
    Elasticsearch 学习笔记
    Prometheus 监控报警系统 AlertManager 之邮件告警
    Match All Query
    Elasticsearch postman
    Centos7修改root密码
    ElasticSearch中profile API的使用
    kafka查询某时间段内的消息
  • 原文地址:https://www.cnblogs.com/hexinxiaoyao/p/2932093.html
Copyright © 2011-2022 走看看