AJAX 和 Codeigniter 控制器

发布于 2024-12-10 07:47:09 字数 1416 浏览 0 评论 0原文

我想动态解析 RSS 提要。 我有一个选择列表,我想使用 ajax 向控制器发送一个值(id)。 之后,我想解析与 id

My Controller home.php 对应的 RSS 提要:

function view($type = NULL)
{
    $data = array();
    $this->load->model('flux_model');

    if ($type == "ajax") {// load ajax view                 
        $flux = $this->flux_model->get_one_flux($this->input->post('id'));// ajax id
        $flux2 = $flux[0]->url_flux;
        $xml = new SimpleXMLElement($flux2, NULL, TRUE);
        //my foreach
        $this->load->view('messages_list', $data);
        }
    else{ // load the default view      
        $nb_min = 0;
        $nb_max = 7;
        $nombre = mt_rand($nb_min,$nb_max);
        $flux = $this->flux_model->get_one_flux($nombre);
        $flux2 = $flux[0]->url_flux;
        $xml = new SimpleXMLElement($flux2, NULL, TRUE);
        //my foreach
        $this->load->view('default', $data);
        }
}

Ajax 脚本:

    $("#myform1 #rss").change(function(){  
    var msg = $('#myform1 #rss').val();

    $.post("<?= site_url('home/view/ajax') ?>", {id: msg}, function() {
        $('#main').load("<?= site_url('home/view/ajax') ?>");
        $('##myform1 #country').val('');
    });

});

查看默认作品,我随机解析 RSS 提要 但使用 ajax 视图时,出现此错误: 消息:SimpleXMLElement::__construct():I/O 警告:无法加载外部实体“”,

看起来我没有获取 id?!阿贾克斯问题?

I want to dynamically parse RSS feeds.
I have a select list and I would like to send a value (id) to the controller with ajax.
and after, I want to parse RSS feeds corresponding to the id

My Controller home.php :

function view($type = NULL)
{
    $data = array();
    $this->load->model('flux_model');

    if ($type == "ajax") {// load ajax view                 
        $flux = $this->flux_model->get_one_flux($this->input->post('id'));// ajax id
        $flux2 = $flux[0]->url_flux;
        $xml = new SimpleXMLElement($flux2, NULL, TRUE);
        //my foreach
        $this->load->view('messages_list', $data);
        }
    else{ // load the default view      
        $nb_min = 0;
        $nb_max = 7;
        $nombre = mt_rand($nb_min,$nb_max);
        $flux = $this->flux_model->get_one_flux($nombre);
        $flux2 = $flux[0]->url_flux;
        $xml = new SimpleXMLElement($flux2, NULL, TRUE);
        //my foreach
        $this->load->view('default', $data);
        }
}

Ajax script :

    $("#myform1 #rss").change(function(){  
    var msg = $('#myform1 #rss').val();

    $.post("<?= site_url('home/view/ajax') ?>", {id: msg}, function() {
        $('#main').load("<?= site_url('home/view/ajax') ?>");
        $('##myform1 #country').val('');
    });

});

view default works, I parse an RSS feed randomly
but with the ajax view, I have this error:
Message: SimpleXMLElement::__construct(): I/O warning : failed to load external entity ""

it looks like I do not get the id?! ajax problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短暂陪伴 2024-12-17 07:47:09

我认为您的问题在于

$.post("<?= site_url('home/view/ajax') ?>" [etc.]

因为许多站点的配置会阻止跨站点 Ajax 请求(请参阅 XSS),您的 $.post 将被阻止。相反,请尝试以下内容:

$.post("controller/method/parameters" [etc.]

示例

在您的 JavaScript 中:

$.post("ajax/myajax/myparam",{},function(data) { alert(data); });

在您的controllers/ajax.php 文件中:

class Ajax extends CI_Controller

    public function __construct()
    {
        parent::__construct();
    }

    public function myAjax(parameter='')
    {
        /**
         * Load models, or whatever. Then echo the results, so that
         * $.post gets its "data" var.
         */
    }

}

我希望这会有所帮助!

I think your issue is with

$.post("<?= site_url('home/view/ajax') ?>" [etc.]

Because many sites' configurations will prevent cross-site Ajax requests (See XSS), your $.post will be prevented. Instead, try something like:

$.post("controller/method/parameters" [etc.]

Example:

In your JavaScript:

$.post("ajax/myajax/myparam",{},function(data) { alert(data); });

And in your controllers/ajax.php file:

class Ajax extends CI_Controller

    public function __construct()
    {
        parent::__construct();
    }

    public function myAjax(parameter='')
    {
        /**
         * Load models, or whatever. Then echo the results, so that
         * $.post gets its "data" var.
         */
    }

}

I hope this helps!

樱花落人离去 2024-12-17 07:47:09

我只是将 : 替换

  $.post("<?= site_url('home/view/ajax') ?>", {id: msg}, function() {
  $('#main').load("<?= site_url('home/view/ajax') ?>");
  $('##myform1 #country').val('');
  });

  $.post('<?= site_url('home/view/ajax') ?>', options,     function(data) {
  $('#content').html(data);
 })

I just replace :

  $.post("<?= site_url('home/view/ajax') ?>", {id: msg}, function() {
  $('#main').load("<?= site_url('home/view/ajax') ?>");
  $('##myform1 #country').val('');
  });

by

  $.post('<?= site_url('home/view/ajax') ?>', options,     function(data) {
  $('#content').html(data);
 })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文