成功后无法刷新页面

时间:2019-04-30 01:38:55

标签: javascript jquery codeigniter

如果条件成功,我会放置刷新代码,但是它没有运行,我被一些代码替换了,但是仍然无法运行

这是控制者

if($result) {
$response = array(
                'success' => true,
                'message' => $this->localization->lang('success', array('name' => $this->localization->lang('task_tickets')))
            );
           $this->redirect->back();
        } else {
            $response = array(
                'success' => false,
                'message' => $this->localization->lang('error_update_message', array('name' => $this->localization->lang('task_tickets')))
            );
        }

这是索引视图

 function reject(id) {
        $.ajax({
            url: '<?= $this->url_generator->current_url() ?>/reject/'+id,
            function(response) {
                    if (response.success) {
                        $.growl.notice({message: response.message});

                    } else {
                        $.growl.error({message: response.message});
                    }
                }
        })

2 个答案:

答案 0 :(得分:0)

如果您正在使用.ajax函数,则对于$_GET请求,AJAX的工作方式如下:

$.ajax({
  method:'GET',
  dataType:'json',
  url:'wherever_you_send.php?prop1=val1',
  success:function(phpJSON){
    console.log(phpJSON.returnProp);
  }
});

现在在wherever_you_send.php上,您需要:

<?php
if(isset($_GET['prop1'])){
  $results = []; 
  $results['returnProp'] = $_GET['prop1']; // add results like this
  echo json_encode($results);
}
?>

顺便说一句,您通常使用AJAX的全部原因是您不必刷新页面...但是,如果必须使用JavaScript location.reload(true)。删除true参数以保留缓存。

答案 1 :(得分:0)

您需要json_encode $response并将其提供给浏览器。使用ajax时,不要在php中重定向。这行不通。

PHP:

    if ($result) {
        $response = array(
            'success' => true,
            'message' => $this->localization->lang('success', array('name' => $this->localization->lang('task_tickets'))),
            'redirect_url' => base_url() . '/some/url'
        );
        //$this->redirect->back();
    } else {
        $response = array(
            'success' => false,
            'message' => $this->localization->lang('error_update_message', array('name' => $this->localization->lang('task_tickets')))
        );
    }
    echo json_encode($response);
    exit;

JQUERY:

function reject(id) {
    $.ajax({
        type: 'get',
        url: '<?= $this->url_generator->current_url() ?>/reject/' + id,
        dataType: 'json',
        function(response) {
            if (response.success) {
                $.growl.notice({message: response.message}); // this message probably won't display due to redirect
                window.location.href = response.redirect_url; // redirect
            } else {
                $.growl.error({message: response.message});
            }
        }
    });
}

请注意:由于您要重定向jquery中的成功消息可能不会显示,或者如果显示,则仅会显示少于一秒钟的时间,具体取决于客户端互联网的速度。我建议添加一条即显消息或一条提示消息,以便在用户单击“确定”时将其重定向。

相关问题