我正在为我的网站使用 codeigniter rest ful api 服务并使用 json 响应。我们用 ajax 实现了它。该代码在 localhost 中完美运行,但无法在线运行。它显示 200 响应但 json 数据未在 ajax 中解析以下是代码
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url();?>news/news.json",
type: "POST",
data: "",
success: function(data, textStatus, jqXHR) {
var news_title, news_id, new_image;
var json = $.parseJSON(JSON.stringify(data));
var str2;
$.each(json, function(idx, obj) {
news_title = obj.news_title_en;
news_id = obj.news_id;
new_image = obj.news_image_en;
str2 = "";
str2 += '<a href="<?php echo base_url();?>news_detail/' + news_id + '">';
if (new_image != '') {
str2 += '<img src="<?php echo base_url();?>admin/uploads/news/small/' + new_image + '" alt="' + news_title + '" /><span style="width:187px;font-size:13px;">' + news_title.substring(0, 22) + '</span>';
} else {
str2 += '<img src="<?php echo base_url();?>images/no-image.jpg" alt="' + news_title + '" /><span style="width:187px;font-size:13px;">' + news_title.substring(0, 22) + '</span>';
}
str2 += '</a>';
$('#news').append(str2);
});
},
error: function(jqXHR, textStatus, errorThrown) {
$('#news').append('No News to display');
}
});
$.ajax({
url: "<?php echo base_url();?>events/events.json",
type: "POST",
data: "",
success: function(data, textStatus, jqXHR) {
var events_id, events_title_en, events_image_en;
var json = $.parseJSON(JSON.stringify(data));
var str3;
$.each(json, function(idx, obj) {
events_title = obj.events_title_en;
events_id = obj.events_id;
events_image = obj.events_image_en;
str3 = "";
str3 = '<a href="<?php echo base_url();?>events_detail/' + events_id + '">';
if (obj.events_image_en != '') {
str3 += '<img src="<?php echo base_url();?>admin/uploads/events/small/' + events_image + '" alt="' + events_title + '" /><span style="width:187px;">' + events_title.substring(0, 22) + '</span>';
} else {
str3 += '<img src="<?php echo base_url();?>images/no-image.jpg" alt="' + events_title + '" /><span style="width:187px;">' + events_title.substring(0, 22) + '</span>';
}
str3 += '';
$('#events').append(str3);
});
},
error: function(jqXHR, textStatus, errorThrown) {
$('#events').append('No Events to display');
}
});
});
对于第一部分新闻,它在在线和本地都运行良好。请让我知道是什么问题
以下是我的控制器代码
public function events_post($id="")
$data=array();
$post=$this->input->post();
$id=$post['id'];
$session_data = $this->session->all_userdata();
$this -> load -> model('events_model', 'Events');
$data=$this->Events->getEvents($id);
$this->response($data);
public function events_detail_get($id="")
$data=array();
$session_data = $this->session->all_userdata();
$data['page_name']='events';
$this->load->view($this->site_lang.'/layouts/header-'.$this->site_lang.'.php',$data);
$this->load->view($this->site_lang.'/events-detail-'.$this->site_lang.'.php',$data);
$this->load->view($this->site_lang.'/layouts/footer-'.$this->site_lang.'.php',$data);