结果按顺序排在第一个ajax但内部循环顺序随机改变。为什么?

时间:2017-01-26 18:23:25

标签: javascript jquery ajax

这对我来说真的很奇怪。我无法理解为什么会这样,我希望有人来解释一下。

好的,我正在运行一个ajax调用,我正在调用[data]并通过for循环运行它,我获取所有数据库值然后我正在运行另一个ajax调用以检查其他条件我在哪里其他价值。一切都很好,因为我想要。问题是在第二个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 个答案:

答案 0 :(得分:1)

你的第二次ajax调用是异步完成的,所以一旦结果被重新显示,它们就会显示出来。您实际上是在不等待任何结果的情况下一个接一个地触发所有ajax调用,并在服务器应答时显示结果。