weixin_33743703 2015-12-26 12:29 采纳率: 0%
浏览 15

getJSON脚本问题

i have a question

This making me crazy

I write a barcode scanner witch check data from json.

The script works fine but

After the while loop i want to get a error alert message if the getjson request dont match with the scanned barcode.

That means if the code dont get into the if(myshopdata.order.reference==barcode)

But every time i put the error alert alert("Code cannot be found"); in my code, the error message comes long before the getjson message.

I know that getJSON is async. and try the same with ajax call , but its to slow it tested it.

Can anyone help me how i solve this in my Code?

The error message is the only thing is missing in my script

This is my script

  • 写回答

1条回答 默认 最新

  • 北城已荒凉 2015-12-26 13:42
    关注

    Following comments... see updated answer using $.when.apply() approach.

    document.addEventListener("intel.xdk.device.barcode.scan", function(evt){
    
        if (evt.success == true) {
          //successful scan
          var barcode = evt.codedata;                             
    
          //Get numbers of Orders
          $.getJSON("http://www.testurl.de/api/orders/?ws_key=TEEUQWL9J76LFQE&output_format=JSON", function(data){                        
    
              var order =  JSON.stringify(data);   
              var orderdata =JSON.parse(order); 
              var ordercount = Object.keys(orderdata.orders).length;
    
              var queries = [];
              var result = 0;
              var i = 0;
    
              while(i <= ordercount)
              {
                queries.push(
    
                    $.getJSON("http://www.testurl.de/api/orders/"+i+"?ws_key=TEZQFW41L9J76LFQE&output_format=JSON", function(data){
    
                      var shopdata = JSON.stringify(data);               
                      var myshopdata =JSON.parse(shopdata);
                      result++; // Increment results counter
                      sweetAlert("Code will be checkd", "Please wait","info");
    
                      if(myshopdata.order.reference == barcode)
                        addcode(myshopdata.order.reference,myshopdata.order.associations.order_rows[0].product_name,myshopdata.order.total_paid_tax_incl,myshopdata.order.associations.order_rows[0].product_id);                         
    
                    });
                );
    
                i++;
              } //end of while loop 
    
              // When all the getJSON calls have completed
              $.when.apply($, queries).done(function() {
    
                  if(!result) // check results
                    console.log('No orders found');
              });
    
          }); //end of get json       
    
        }//end of event listener if 
        else
          sweetAlert("Hoppla", "Woltest du nicht gerade scannen? :) ");
    
    },false);//end of event listener
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助