Refreshing a page with codeigniter after ajax click event

北城以北 提交于 2019-12-25 03:19:22

问题


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.



来源:https://stackoverflow.com/questions/25632701/refreshing-a-page-with-codeigniter-after-ajax-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!