【问题标题】:Load content via AJAX in jQuery (CodeIgniter view)在 jQuery 中通过 AJAX 加载内容(CodeIgniter 视图)
【发布时间】:2012-05-20 00:38:16
【问题描述】:

我在这个链接找到了一个关于如何在 jQuery 中使用 AJAX 的好例子: http://yensdesign.com/2008/12/how-to-load-content-via-ajax-in-jquery/

我将index.html 文件中的内容添加到我的view.php 文件中(我正在使用CodeIgniter)。

一切看起来都不错,但是当您想切换到另一个部分时,页面内容不会改变(显示与之前相同的内容)。

我认为可能有问题,menu.js 文件将数据传递给 CodeIgniter view.php 文件?

你能给我一些关于如何解决这个问题的建议吗?我希望这对其他人也有帮助。

Menu.js 文件:

$(document).ready(function(){
//References
var sections = $("#menu li");
var loading = $("#loading");
var content = $("#content");

//Manage click events
sections.click(function(){
    //show the loading bar
    showLoading();
    //load selected section
    switch(this.id){
        case "home":
            content.slideUp();
            content.load("sections.html #section_home", hideLoading);
            content.slideDown();
            break;
        case "news":
            content.slideUp();
            content.load("sections.html #section_news", hideLoading);
            content.slideDown();
            break;
        case "interviews":
            content.slideUp();
            content.load("sections.html #section_interviews", hideLoading);
            content.slideDown();
            break;
        case "external":
            content.slideUp();
            content.load("external.html", hideLoading);
            content.slideDown();
            break;
        default:
            //hide loading bar if there is no selected section
            hideLoading();
            break;
    }
});

//show loading bar
function showLoading(){
    loading
        .css({visibility:"visible"})
        .css({opacity:"1"})
        .css({display:"block"})
    ;
}
//hide loading bar
function hideLoading(){
    loading.fadeTo(1000, 0);
};

});

来自 javascript 的动画正在运行 (loading/content/sections) ,但实际内容并未从 html 文件中加载。

正如我所说,这个脚本似乎无法将内容加载到 view.php 文件(即 CodeIgniter 'view' 文件)

非常感谢任何帮助。

【问题讨论】:

  • javascript 在哪里? php?
  • 您可以在该链接中找到的所有文件。在我的配置中: Javascript 文件位于 apache 服务器 www 目录的 /scripts 文件夹中,view.php 文件位于 /application/view/lab/view.php 文件夹中。
  • @jomajo - RPM 要求提供 您的 代码,而不是库代码或示例代码。当我们没有您的代码时,您希望有人如何解决您的问题?您是否要求您的机械师通过将汽车锁在车库中并隐藏钥匙然后只是让他参考技术手册来修理您的汽车?可能不是苹果与苹果之间的比较,但我希望这对你有意义,如果你想让人们回答你的问题,你必须向他们展示你正在使用的代码。 :)
  • 嗨,已添加代码。正如我所说,代码没有被修改,我只是想知道,为什么那个脚本不能将数据传递给 VIEW,以及我们如何解决这个问题。谢谢

标签: jquery ajax codeigniter


【解决方案1】:

CodeIgniter 需要服务于sections.htmlexternal.html。默认情况下不会这样做。

假设view.php 用于URL http://example.com/index.php/mycontroller/view,那么jQuery 将发送对以下URL 的请求。

http://example.com/index.php/mycontroller/sections.html
http://example.com/index.php/mycontroller/external.html

除非您告诉 CodeIgniter 对这些请求执行什么操作,否则 CodeIgniter 将返回 404 并且您的页面内容不会更新。

【讨论】:

  • 但如果我添加 tihs 行,在 javascript 中:content.load("<?=base_url()?>sections.html #section_home", hideLoading); 没有任何变化。没有 404 错误。其他部分内容与第一部分相同。此外,如果我将函数 section 添加到我的控制器,使用 echo 语句,幻灯片就会上升,并且什么也没有发生。
  • 太棒了,我解决了问题:D 刚刚添加到我的控制器:function sections() { $this->load->view('view/sections.html'); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
相关资源
最近更新 更多