dongqiang8683 2014-11-17 07:05
浏览 49
已采纳

使用ajax和不使用ajax的Codeigniter-控制器视图

I have a default controller:

class Home extends CI_Controller {

public function index() 
{   
    $data['content']='discover';
    $this->load->view('layouts/master', $data);             
}   

public function categories()
{
    $this->load->view('categories');
}

}

On click, using AJAX i was able to load the view categories.php and update the URL to www.mywebsite.com/categories without refreshing the page.

But now, if the user lands here directly using the link or refreshes the page he ends up loading only the categories.php view and not the master.php view

  1. How do i load the master.php and categories.php view on direct URL navigation or page refresh

  2. Just load categories.php when using AJAX.

My AJAX:

$('#navbar a').click(function(e){
    e.preventDefault();
    href=$(this).attr('href');
    if (href.indexOf(document.domain) > -1 || href.indexOf(':') === -1){
        window.history.pushState('obj', '', href);
        navigat(href);          
    }       
});

function navigat(href){             
    $.ajax({
        type:'GET',
        url:href,
        cache:false,
        dataType: 'html',
        statusCode: {
                404: function() {
                  $("#content").html('Could not contact server.');
                },
                500: function() {
                  $("#content").html('A server-side error has occurred.');
                }
        },
        error: function() {
            $("#content").html('A problem has occurred.');
        },
        success: function(html) {
            $('#content').html(html);

        }
    });     
};

Thanks!

  • 写回答

2条回答 默认 最新

  • dongpo5264 2014-11-17 08:46
    关注

    The CodeIgniter Input Class Provides some helper functions to deal with user inputs , this class is initialized automatically by the system so there is no need to do it manually.

    I always recommend to use this class instead using PHP Core Input Variables while dealing with forms and requests.

    To check Ajax Request in CodeIgniter, codeIgniter Output Class Method class is_ajax_request() , this function checks to see if the HTTP_X_REQUESTED_WITH server header has been set, if set response will be true otherwise false means it will returns a boolean response.

    In you controller you can use below code

    if (!$this->input->is_ajax_request()) {
            echo "Not ajax request";
    } else {
            echo "Ajax Request";
    }
    

    Hence using the above script in your controller action you can identify whether user is coming directly or from ajax call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵