2017-08-29 258 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(); 
    }