2017-08-29 208 views
0

我想笨框架,AJAX功能重定向到網頁中的AJAX數據重定向到頁面笨

我在我的第一個頁面的Ajax功能我傳遞一些輸入值通過Ajax執行我的行動後,控制器功能想同樣的功能視圖頁面重定向

我的AJAX功能看起來像

function search() 
{ 
    var city = $('input[name="city"]').val(); 
    var location = $("#location").val(); 
    var cat = $("#cat").val(); 

    jQuery.ajax({ 
     url:'<?php echo base_url()."Fitness/studios"; ?>', 
     type: 'POST', 
     data:'city='+ city + '&location='+ location + '&cat='+ cat , 
     cache:false, 
     async:false, 
     success: function (data) { 
      if (data.status == 'success'){ 
       window.location = "<?php echo base_url();"Fitness/studios"?>"; 
      } 
     } 

    }); 
} 

和我的控制器功能是

public function studios() 
{ 
    $city = $_POST['city']; 
    $location = $_POST['location']; 
    $cat = $_POST['cat']; 
    $data['studios']= $this->Welcome_model->selectstudios($city,$location,$cat); 

    if($data['studios']) 
    { 
     $data['status']= "success"; 
    } 
    $this->load->view('studiolist', $data); 
} 

$data['studios']未能就$this->load->view('studiolist', $data);

回答

0

重定向頁面在控制器中獲得價值後u需要做的回聲

if($data['studios']) 
{ 
    echo "success"; 
} 
else{ 
    echo "Failed"; 
} 

,並在Ajax響應u需要做這樣的 後數據這樣的

data: {location:$("#location").val(), 
city:$("#city").val(), 
cat:$("#cat").val()}, 
if (data == 'success'){ 
      window.location = "ur location to redirect"; 
     } 

控制器訪問這樣的數據

$this->input->post('fieldname'); insetead of $_post['fieldname']; 

並從控制器

​​
+0

感謝ü但是這不是爲我工作 – Tejaswee

+0

張貼這樣 – Shibon

+0

@Tejaswee數據後嘗試:這將工作你,可能是你錯過了一些東西。 – kishor10d

0

這一行你應該嘗試像波紋管,我希望它會解決你的問題

$view= $this->load->view('studiolist', $data,TRUE); 
$jData=array('data'=>$data,'view'=>$view); 
echo json_encode($jData); 

通視圖後,您可以在阿賈克斯的成功功能檢查

success: function (data) { 
console.log("data",data) 
      /*if (data.status == 'success'){ 
       window.location = "<?php echo base_url();"Fitness/studios"?>"; 
      } */ 
     } 
0

有一個重定向url helper fun ction在笨這可能不是在這種情況下工作,以便創建一個自定義的輔助函數來過寫:

/** 
    * Like CodeIgniter redirect(), but uses javascript if needed to redirect out 
    * of an ajax request. 
    * 
    * @param string $url The url to redirect to. If not a full url, will wrap it 
    * in site_url(). 
    * 
    * @return void 
    */ 
    public static function redirect($url = null) 
    { 
     if (! preg_match('#^https?://#i', $url)) { 
      $url = site_url($url); 
     } 

     if (! self::$ci->input->is_ajax_request()) { 
      header("Location: {$url}"); 

      // The default header specifies the content type as HTML, which requires 
      // certain elements to be considered valid. No content is included, 
      // so use a content type which does not require any. 
      header("Content-Type: text/plain"); 
     } else { 
      // Output URL in a known location and escape it for safety. 
      echo '<div id="url" data-url="'; 
      e($url); 
      echo '"></div>'; 

      // Now JS can grab the URL and perform the redirect. 
      echo <<<EOF 
<script> 
window.location = document.getElementById('url').getAttribute('data-url'); 
</script> 
EOF; 
     } 

     exit(); 
    }