首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ajax codeigniter中重定向到包含数据的页面

在ajax codeigniter中重定向到包含数据的页面
EN

Stack Overflow用户
提问于 2017-08-29 18:25:31
回答 3查看 1.1K关注 0票数 0

我想在codeigniter框架中重定向到具有ajax功能的页面

我有ajax函数在我的第一个页面,我正在通过ajax传递一些输入值到控制器函数后,执行动作,我想要重定向到相同的函数视图页面

我的ajax函数如下所示

代码语言:javascript
复制
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"?>";
            } 
        }

    });
}

我的控制器函数是

代码语言:javascript
复制
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);上重定向页面

EN

回答 3

Stack Overflow用户

发布于 2017-08-29 18:31:14

在控制器中,你需要做回显

代码语言:javascript
复制
if($data['studios'])
{
  echo "success";  
}
else{
  echo "Failed";
}

在ajax响应中,你需要像这样做post数据

代码语言:javascript
复制
data: {location:$("#location").val(),
city:$("#city").val(),
cat:$("#cat").val()},
if (data == 'success'){
            window.location = "ur location to redirect";
        }

在控制器中,像这样访问数据

代码语言:javascript
复制
$this->input->post('fieldname'); insetead of $_post['fieldname'];

并从控制器中删除此行

代码语言:javascript
复制
$this->load->view('studiolist', $data);
票数 0
EN

Stack Overflow用户

发布于 2017-08-29 18:44:47

你应该试一试,我希望它能解决你的问题

代码语言:javascript
复制
$view= $this->load->view('studiolist', $data,TRUE);
$jData=array('data'=>$data,'view'=>$view);
echo json_encode($jData);

通过查看后,您可以检入ajax的成功函数

代码语言:javascript
复制
success: function (data) {
console.log("data",data)
            /*if (data.status == 'success'){
                window.location = "<?php echo base_url();"Fitness/studios"?>";
            } */
        }
票数 0
EN

Stack Overflow用户

发布于 2017-08-29 20:15:10

在codeigniter中有一个重定向url助手函数,在这种情况下可能不起作用,所以创建一个自定义助手函数来覆盖它:

代码语言:javascript
复制
/**
     * 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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45936694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档