AJAX 和 Codeigniter 控制器
我想动态解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的问题在于
因为许多站点的配置会阻止跨站点 Ajax 请求(请参阅 XSS),您的 $.post 将被阻止。相反,请尝试以下内容:
示例:
在您的 JavaScript 中:
在您的controllers/ajax.php 文件中:
我希望这会有所帮助!
I think your issue is with
Because many sites' configurations will prevent cross-site Ajax requests (See XSS), your $.post will be prevented. Instead, try something like:
Example:
In your JavaScript:
And in your controllers/ajax.php file:
I hope this helps!
我只是将 : 替换
为
I just replace :
by