2017-01-26 2 views
1

これは本当に私にとっては変です。私はなぜこれが起こっているのか理解できません、私は誰かがこれを説明してほしいです。最初のajaxではなく、forループ内の順序がランダムに変更されます。どうして?

私は[データ]を呼び出していて、すべてのデータベース値を取得しているforループを実行している1つのajax呼び出しを実行しています。別のajax呼び出しを実行して、他の価値を得る。私が望むようにすべてがうまくいっています。問題は、2番目のajaxが成功した後に値を取得する順序がランダムであることです。それは全く理解できません。

私の最初のajax呼び出しでは、正しい順序、すなわちDESC順序で結果が得られています。

理由を教えてください。

function get_filter_data(val) { 
      $.ajax({ 
       type: "POST", 
       url: "filter_data.php", 
       dataType: 'json', 
       data:'rating='+val, 
       success: function(data){ 

        var str = ''; 
        var cide = $('#cSessionid').val(); 
        //console.log(cide+'-session-id'); 

        for(var i in data) 
        { 
         var ide = data[i].id; 
         var username = data[i].username; 
         var u_name = username.toLowerCase().replace(/\b[a-z]/g, function(letter) { 
          return letter.toUpperCase(); 
         }); 
         var email = data[i].email; 
         var ref_number = data[i].ref_number; 
         var rating = data[i].rating; 
         var title = data[i].review_title; 
         var desc = data[i].review_desc; 
         var pubdate = data[i].pubdate; 


         //console.log(data[i].id+'-first-id'); 
         $.ajax({ 
          type: "POST", 
          url: "add_edit_delete.php", 
          dataType: 'json', 
          cache: false, 
          data: { 
           username: username, 
           u_name: u_name, 
           email: email, 
           ref_number : ref_number, 
           rating : rating, 
           title : title, 
           desc : desc, 
           pubdate : pubdate, 
           ppid : ide 
           }, 
          success: function(data){ 

           var eip = data.ipadt; 

           if(eip === cide){ 

          var ddc = '<div class="editndeletebox"><a href="#" class="editreviewn"><span>Edit</span></a><a class="deletereviewn" href="#"><span>Delete</span></a></div>'; 

         }else{ 

          var ddc = '<span></span>'; 
         } 

         str += '<div class="review pageable-item-js item clearfix" data-reviewmid="57c5e5e20000ff000981e1be">'; 
         str +=  '<div class="user-info clearfix">'; 
         str +=   '<div class="user-review-name clearfix"><a class="user-review-name-link" rel="nofollow" title="go to ' + data.u_name + ' profile" href="../users/users.php?review='+data.pid+'">'; 
         str +=    data.u_name; 
         str +=   '</a></div>'; 
         str +=   '<div class="clearfix">1 review </div>'; 
         str +=  '</div>'; 
         str +=  '<div class="review-info clearfix">'; 
         str +=  '<input type="hidden" id="dneid" value="'+data.pid+'" />'; 
         str +=   ddc; 
         str +=   '<div class="star-rating count-'+data.rating+' size-medium clearfix">'; 
         str +=    '<div class="star-1"><img src="../community/shared/sprite_star.png" class="star-image" alt=""></div>'; 
         str +=    '<div class="star-2"><img src="../community/shared/sprite_star.png" class="star-image" alt=""></div>'; 
         str +=    '<div class="star-3"><img src="../community/shared/sprite_star.png" class="star-image" alt=""></div>'; 
         str +=    '<div class="star-4"><img src="../community/shared/sprite_star.png" class="star-image" alt=""></div>'; 
         str +=    '<div class="star-5"><img src="../community/shared/sprite_star.png" class="star-image" alt=""></div>'; 
         str +=   '</div>'; 


         var publish_date = $.timeago(data.pubdate); 
         var publish_date = $.trim(publish_date); 


         // Published Wednesday, October 24, 2012  

         if(publish_date != "false") { 
          show_date = publish_date; 
         } 
         else { 

          var dayNames = [ 
           "Sunday", "Monday", "Tuesday", 
           "Wednesday", "Thursday", "Friday", "Saterday" 
          ]; 

          var monthNames = [ 
           "January", "February", "March", 
           "April", "May", "June", "July", 
           "August", "September", "October", 
           "November", "December" 
          ]; 

          var date = new Date(data.pubdate); 
          var day = date.getDate(); 
          var monthIndex = date.getMonth(); 
          var year = date.getFullYear(); 
          var dayIndex = date.getDay(); 

          var show_date = 'Published ' + dayNames[dayIndex] + ', ' + monthNames[monthIndex] + ' ' + day + ', ' + year; 
         } 


         str +=  '<time datetime="'+data.pubdate+'" class="ndate timeago" title="'+data.pubdate+'" >'; 
         str +=  show_date + '<span title=""></span>'; 
         str +=  '</time>'; 

         if (data.ref_number == 11) { 
          str += '<div class="hover_tip"><div class="review-verified"><div class="review-verified-tooltip-text-hidden">'+ data.u_name +' was invited to write this review by The Logo Company. <a href="https://support.trustpilot.com/hc/articles/201819697-?utm_medium=consumer&utm_source=verified_order_learn_more&utm_campaign=consumer_verified_order" rel="nofollow" class="review-verified-tooltip-link">Learn more</a>.</div><span class="icon icon-ok-sign"></span> Verified order<div class="review-verified-tooltip-container" style="width: 421px;"><p class="review-verified-tooltip-text">'+data.u_name+' was invited to write this review by The Logo Company. <a href="https://support.trustpilot.com/hc/articles/201819697-?utm_medium=consumer&utm_source=verified_order_learn_more&utm_campaign=consumer_verified_order" rel="nofollow" class="review-verified-tooltip-link">Learn more</a>.</p></div></div></div>'; 
         } 
         else { 

          str += '<div class="hover_tip"><div class="review-verified"><div class="review-verified-tooltip-text-hidden">Thelogocompany.net were not able to verify which order correlates to this review. <a href="https://support.trustpilot.com/hc/articles/201819697-?utm_medium=consumer&utm_source=verified_order_learn_more&utm_campaign=consumer_verified_order" rel="nofollow" class="review-verified-tooltip-link">Learn more</a>.</div><span class="icon icon-ok-sign"></span> Unverified order<div class="review-verified-tooltip-container" style="width: 421px;"><p class="review-verified-tooltip-text">Thelogocompany.net were not able to verify which order correlates to this review. <a href="https://support.trustpilot.com/hc/articles/201819697-?utm_medium=consumer&utm_source=verified_order_learn_more&utm_campaign=consumer_verified_order" rel="nofollow" class="review-verified-tooltip-link">Learn more</a>.</p></div></div></div>'; 
         } 
         //<span class="icon icon-ok-sign"></span> 





         str +=  '<h3 class="review-title en h4">'; 
         str +=   '<a class="review-title-link" rel="nofollow" href="../reviews/reviews.php?review='+data.pid+'">'; 
         str +=   data.title;          
         str +=   '</a></h3><div class="review-body">'; 
         str +=  data.desc; 
         str +=  '</div>'; 
         str +=   '<div class="review-actions clearfix"><div class="btn-action clearfix "><div class="icon icon-thumbs-up circle-action blue-action" id="pressme_'+data.pid+'" onclick="funn('+data.pid+')"; title="Find useful" ><i class="fa fa-thumbs-up" aria-hidden="true"></i></div><div class="number-box" id="box_'+data.pid+'" style="display:none"><div class="text useful-counter-js" id="val_'+data.pid+'"></div></div></div><div class="btn-action clearfix" ><div class="icon icon-report circle-action notify" id='+data.pid+' title="Notify Compliance" onclick="fun('+data.pid+');"><i class="fa fa-flag" aria-hidden="true"></i></div></div><div class="btn-action clearfix" data-social-share-url="https://www.trustpilot.com/reviews/57c5e5e20000ff000981e1be" data-event-source="CompanyProfile" data-shared-object="SellerReview"><div class="icon icon-facebook circle circle-action social-share-facebook-js" title="Share on Facebook"><i class="fa fa-facebook" aria-hidden="true"></i></div></div><div class="btn-action clearfix" data-social-share-url="https://www.trustpilot.com/reviews/57c5e5e20000ff000981e1be" data-event-source="CompanyProfile" data-shared-object="SellerReview"><div class="icon icon-google circle circle-action social-share-google-js" title="Share on Google+"><i class="fa fa-google-plus-official" aria-hidden="true"></i></div></div><div class="btn-action clearfix" data-social-share-url="https://www.trustpilot.com/reviews/57c5e5e20000ff000981e1be" data-status="DanielleB gave The Logo Company 5 stars via @TrustpilotUS [Link]" data-event-source="CompanyProfile" data-shared-object="SellerReview"><div class="icon icon-twitter circle circle-action social-share-twitter-js" title="Share on Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></div></div><div class="review-action-response" ><ul class="reporting hide" id="report_'+data.pid+'" style="display: none;"><li class="first">Dear Guest. You just clicked on Notify Compliance because you believe that this review is violating Trustpilot’s <a href="#" class="review-guidelines" data-url="http://legal.trustpilot.com/user-guidelines">User Guidelines</a>.<br><br><i>If you are a business user and you wish to report a review on your company profile, please do so on your <a href="https://en-us.b2b.trustpilot.com/Login?goto=http%3a%2f%2fen-us.b2b.trustpilot.com%2fng%2fdashboard" target="_blank" rel="nofollow">business account</a>. Don’t have an account? It’s free and easy to <a href="http://business.trustpilot.com/signup" rel="nofollow" target="_blank">sign up</a>.</i></li><li><label for="message">Please provide us with as much information as possible in the text field provided below (min. 50 characters).</label><div class="alert alert-warning hidden">Please use a minimum of 50 characters.</div><textarea name="message" id="message" class="form-control message" placeholder="Describe why you believe this review requires our Compliance Team’s attention. Does it contain e.g. defamatory statements, coarse language, or does it seem fake?"></textarea><a href="https://www.trustpilot.com/users/connect?signup=True"><input type="submit" value="Submit notification to Compliance" class="btn btn-primary btn-block btn-lg buttonsubmit-js"></a></li></ul><div class="find-useful hidden"></div></div></div></div></div>'; 
         //str +=  '<div class="review-actions clearfix"><div class="btn-action clearfix "><div class="icon icon-thumbs-up circle-action blue-action" id="pressme_'+id+'" onclick="funn('+id+')"; title="Find useful" ><i class="fa fa-thumbs-up" aria-hidden="true"></i></div><div class="number-box" id="box_'+id+'" style="display:none"><div class="text useful-counter-js" id="val_'+id+'"></div></div></div><div class="btn-action clearfix" ><div class="icon icon-report circle-action notify" id='+id+' title="Notify Compliance" onclick="fun('+id+');"><i class="fa fa-flag" aria-hidden="true"></i></div></div><div class="btn-action clearfix" data-social-share-url="https://www.trustpilot.com/reviews/57c5e5e20000ff000981e1be" data-event-source="CompanyProfile" data-shared-object="SellerReview"><div class="icon icon-facebook circle circle-action social-share-facebook-js" title="Share on Facebook"><i class="fa fa-facebook" aria-hidden="true"></i></div></div><div class="btn-action clearfix" data-social-share-url="https://www.trustpilot.com/reviews/57c5e5e20000ff000981e1be" data-event-source="CompanyProfile" data-shared-object="SellerReview"><div class="icon icon-google circle circle-action social-share-google-js" title="Share on Google+"><i class="fa fa-google-plus-official" aria-hidden="true"></i></div></div><div class="btn-action clearfix" data-social-share-url="https://www.trustpilot.com/reviews/57c5e5e20000ff000981e1be" data-status="DanielleB gave The Logo Company 5 stars via @TrustpilotUS [Link]" data-event-source="CompanyProfile" data-shared-object="SellerReview"><div class="icon icon-twitter circle circle-action social-share-twitter-js" title="Share on Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></div></div><div class="review-action-response" ><ul class="reporting hide" id="report_'+id+'" style="display: none;"><li class="first">Dear Guest./*'+username+'*/ You just clicked on Notify Compliance because you believe that this review is violating Trustpilot’s <a href="#" class="review-guidelines" data-url="http://legal.trustpilot.com/user-guidelines">User Guidelines</a>.<br><br><i>If you are a business user and you wish to report a review on your company profile, please do so on your <a href="https://en-us.b2b.trustpilot.com/Login?goto=http%3a%2f%2fen-us.b2b.trustpilot.com%2fng%2fdashboard" target="_blank" rel="nofollow">business account</a>. Don’t have an account? It’s free and easy to <a href="http://business.trustpilot.com/signup" rel="nofollow" target="_blank">sign up</a>.</i></li><li><label for="message">Please provide us with as much information as possible in the text field provided below (min. 50 characters).</label><div class="alert alert-warning hidden">Please use a minimum of 50 characters.</div><textarea name="message" id="message" class="form-control message" placeholder="Describe why you believe this review requires our Compliance Team’s attention. Does it contain e.g. defamatory statements, coarse language, or does it seem fake?"></textarea><a href="https://www.trustpilot.com/users/connect?signup=True"><input type="submit" value="Submit notification to Compliance" class="btn btn-primary btn-block btn-lg buttonsubmit-js"></a></li></ul><div class="find-useful hidden"></div></div></div></div></div>'; 

         $(".reviews_container").html(str); 



          }, 
          error: function(request, status, error) { 
           console.log(error); 
           } 

         });//inner ajax ends 




        } 

       } 
      }); 



     } 

答えて

1

ajax呼び出しの2番目のboutは非同期的に行われるため、結果が再取得されるとすぐに表示されます。あなたは本質的に、結果を待つことなく、サーバーが応答したときに結果を示すことなく、すべてのajax呼び出しを次々に実行しています。

+0

ありがとうございました。どのように私はこのことを止めることができますか教えてください? –

+0

あなたの提案の後、私はasyncを試してみました:false、それは今のところうまくいきます。しかし、私はまだ他の解決策を知りたい。 async:falseは良い解決策ではありません。 –

+0

完全なアプリを見ずに言うのは難しいですが、ループしてajaxを呼び出すのは一般的には悪い考えです。最初のajaxクエリーで必要なデータをすべて返すことができるはずです。 PHPで重い作業を行い、 "item"ごとに必要なすべての情報を保持するオブジェクトを渡します。 –

関連する問題