我想在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);上重定向页面
发布于 2017-08-29 18:31:14
在控制器中,你需要做回显
if($data['studios'])
{
echo "success";
}
else{
echo "Failed";
}在ajax响应中,你需要像这样做post数据
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);发布于 2017-08-29 18:44:47
你应该试一试,我希望它能解决你的问题
$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"?>";
} */
}发布于 2017-08-29 20:15:10
在codeigniter中有一个重定向url助手函数,在这种情况下可能不起作用,所以创建一个自定义助手函数来覆盖它:
/**
* 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();
}https://stackoverflow.com/questions/45936694
复制相似问题