我想在codeigniter框架中重定向到带有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 :(得分:0)
在控制器中你需要做回声
if($data['studios'])
{
echo "success";
}
else{
echo "Failed";
}
并且在ajax响应中你需要这样做 发布这样的数据
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'];
并从控制器中删除此行
$this->load->view('studiolist', $data);
答案 1 :(得分:0)
你应该尝试下风,我希望它能解决你的问题
$view= $this->load->view('studiolist', $data,TRUE);
$jData=array('data'=>$data,'view'=>$view);
echo json_encode($jData);
传递视图后,您可以检查ajax的成功功能
success: function (data) {
console.log("data",data)
/*if (data.status == 'success'){
window.location = "<?php echo base_url();"Fitness/studios"?>";
} */
}
答案 2 :(得分:0)
在codeigniter中有一个重定向url helper函数,在这种情况下可能无效,所以创建一个自定义帮助函数来覆盖它:
/**
* 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();
}