2015-06-17 107 views
0

當我們中止ajax請求時,我正在從PHP服務器上延遲ajax響應。 我正在爲服務器腳本使用codeigniter框架。來自服務器的延遲響應,同時中止jquery ajax

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); 

      } 
     }) 

登錄Controllor

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

+0

考慮兩件事情會更快的查詢1.使用查詢like'select COL1,col2'不'選擇*',2,使用數組返回的結果,而不是目的。數組比對象快。 問題:您可以顯示請求的時間嗎? –

+0

沒有ajax中止它需要-1076ms。與ajax中止它需要:22925ms,還包括顯示時間框架的屏幕截圖。 –

+0

你的'pconnect'設置是什麼?在database.php –

回答

0

這將加速LITTEL,如果你用你的查詢關鍵詞,比如下面

混帳只有一個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"; 
    } 

     } 
+0

只有當ajax中止時纔會很慢,在其他情況下,它沒有任何問題。 –