Refreshing a page with codeigniter after ajax clic

2019-08-29 04:41发布

问题:

I have the following php codeigniter function code which is being called by a jquery ajax function:

            $this->load->library('session');
            $this->load->helper('url');

              $ids = $_POST['i'];
              $message = $_POST['m'];
              var_dump($message);
              var_dump($ids);
              $sql = 'update replies set `response` = "'.$message.'" where id IN ('.implode( ',', $ids).')'; 
              echo $sql;
              R::exec($sql); // works normally to here


              redirect($this->uri->uri_string());

I want to refresh the page after the db insertion of 'message'. however nothing seems to happen. everything works normally including the db insertion. What am I doing wrong?

回答1:

You can not redirect via AJAX url. Redirect is possible using callback function in three ways done, fail and always.

Example:

$.ajax({
        url: '/path/to/file',
        type: 'default GET (Other values: POST)',
        dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
        data: {param1: 'value1'},
    })
    .done(function(url) { // echo url in "/path/to/file" url
        // redirecting here if done
        location.href = url;
    })
    .fail(function(url) { // echo url in "/path/to/file" url
        // redirecting here if failed
        location.href = url;
    })
    .always(function(url) { // echo url in "/path/to/file" url
        // redirecting here always
        location.href = url;
    });


回答2:

On your ajax success use this after showing success message

window.location="path_to redirect";

like for example i am redirecting to member controller

window.location="member";

Hope this helps.