来自服务器的迟到响应,同时中止jquery ajax

时间:2015-06-17 11:40:59

标签: javascript php ajax codeigniter

当我们中止ajax请求时,我从PHP服务器得到了迟到的ajax响应。 我正在使用codeigniter框架作为服务器脚本。

Javascript代码:

cblcurrentRequest = $.ajax({
            url: baseurl + 'Login/getChannelBrand/' + brand_id, 
            type: 'post', 
            async: true, 
            cache: true, 
            data: '',
            beforeSend: function () {
                    $('#tw').html("");
                    $('#fb').html("");
                    $('#inst').html("");
                    $('#yt').html("");
                if (cblcurrentRequest != null) {
                    cblcurrentRequest.abort();
                }
                if (firsttwcurrentRequest != null) {
                    firsttwcurrentRequest.abort();
                }
                if (firstfbcurrentRequest != null) {
                    firstfbcurrentRequest.abort();
                }
                if (firstigcurrentRequest != null) {
                    firstigcurrentRequest.abort();
                }
                if (firstytcurrentRequest != null) {
                    firstytcurrentRequest.abort();
                }

                if (twcurrentRequest != null) {
                    twcurrentRequest.abort();
                }
                if (fbcurrentRequest != null) {
                    fbcurrentRequest.abort();
                }
                if (igcurrentRequest != null) {
                    igcurrentRequest.abort();
                }
                if (ytcurrentRequest != null) {
                    ytcurrentRequest.abort();
                }

            },
            success: function (result) {

                var channelBrandList = JSON.parse(result);
                facebookBrandName = channelBrandList['FacebookChannel'];
                twitterBrandName = channelBrandList['TwitterChannel'];
                youtubeBrandName = channelBrandList['YoutubeChannel'];
                instagramBrandName = channelBrandList['InstagramChannel'];
                YelpBrandName = channelBrandList['YelpChannel'];

                console.log(facebookBrandName);
                console.log(twitterBrandName);
                console.log(youtubeBrandName);
                console.log(instagramBrandName);
                console.log(yelpBrandName);

            }
        })

登录控制器

function getChannelBrand($brandID) {

        $channelBrandList = $this->Model_brand->getChannelBrandList($brandID);

        $channelbrand_array = array('BrandId' => $channelBrandList[0]->BrandId,
            'BrandName' => $channelBrandList[0]->BrandName,
            'FacebookChannel' => $channelBrandList[0]->FacebookChannel,
            'TwitterChannel' => $channelBrandList[0]->TwitterChannel,
            'YoutubeChannel' => $channelBrandList[0]->YoutubeChannel,
            'InstagramChannel' => $channelBrandList[0]->InstagramChannel,
            'YelpChannel' => $channelBrandList[0]->YelpChannel
        );

        echo(json_encode($channelbrand_array));

    }

型号代码:

 function getChannelBrandList($brandID) {
        $query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
        if (count($query->result()) > 0) {
            return $query->result();
        } else {
            return $query = "";
        }
    }

Json回复:

{
 "BrandId":"26",
 "BrandName":"jeep",
 "FacebookChannel":"jeep",
 "TwitterChannel":"jeep",
 "YoutubeChannel":"thejeepchannel",
 "InstagramChannel":"jeep",
 "YelpChannel":null
}

控制台日志:

enter image description here

如果在框架级别或其他任何方面需要任何配置,请建议。

谢谢, Sameek

1 个答案:

答案 0 :(得分:0)

如果你使用下面的查询

,这将加速

仅抓取一个row并使用num_rows()计算行数并return false

<强>模型

<?php
function getChannelBrandList($brandID) {
        $query = $this->db->query('SELECT * FROM brands where BrandId='.$brandID);
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

<强>控制器

function getChannelBrand($brandID) {
   $channelBrandList = $this->Model_brand->getChannelBrandList($brandID);
    if($channelBrandList){
            $channelbrand_array = array('BrandId' => $channelBrandList->BrandId,
                'BrandName' => $channelBrandList->BrandName,
                'FacebookChannel' => $channelBrandList->FacebookChannel,
                'TwitterChannel' => $channelBrandList->TwitterChannel,
                'YoutubeChannel' => $channelBrandList->YoutubeChannel,
                'InstagramChannel' => $channelBrandList->InstagramChannel,
                'YelpChannel' => $channelBrandList->YelpChannel
            );

            echo(json_encode($channelbrand_array));
    }
    else{
       echo "error";
    }

        }