我正在使用echo json_encode($ data);将数组发送回jquery ajax。我想从json中获取数据并在输入文本上检索它,并且出现如下错误:您提交的URI不允许使用字符。我不知道如何在模型中传递数据。顺便说一句,我正在使用codeigniter。
单击编辑按钮后,将出现一个模态。请帮帮我!!
按钮
<a onclick="edit_content_by_id('.$row->post_id.')" title="Edit"><span class="ti-pencil"></span>
**Model**
public function GetContentById($id) {
$query = $this->db->select('*')->from('cms_posts_tbl cpt')->
join('cms_contents_tbl cct', 'cct.post_id = cpt.post_id')->
join('cms_category_tbl ccat', 'ccat.post_category_id = cpt.post_category')->
where('post_id', $id)->get();
foreach($query as $row) {
$data = $row;
}
echo json_encode($data);
}
**jQuery/AJAX**
function edit_content_by_id(id) {
var data = { content_id : id };
$.ajax({
type: 'POST', url: 'http://localhost:81/ci_sample/model/GetContentById('+ id +')',
data: data, dataType: 'json',
cache: false,
success:function(data) {
$('#modal_content').modal('show');
$('#modal_content').find($('post_title')).val(data.post_title);
}
});
}
答案 0 :(得分:0)
您不能在ajax调用上直接调用模型函数。首先,您必须创建一个控制器
void ReadTextFile(char argv[],Cplex &a,Cplex &b);
像这样更改GetContentById函数:
void ReadTextFile(char argv[],Cplex& a,Cplex& b)
{
std::ifstream fin;
fin.open("complex.txt");
char i;
fin>>a.real>>a.image>>i; // values filled here can now be read in the caller i.e main.
fin>>b.real>>b.image>>i;
std::cout<<std::noshowpos<<a.real<<std::showpos<<a.image<<"i"<<std::endl;
std::cout<<std::noshowpos<<b.real<<std::showpos<<b.image<<"i"<<std::endl;
return;
};
最后,您的ajax调用将如下所示:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class myController extends CI_Controller {
public function fetchData() {
$content_id = $_POST['content_id '];
if($content_id){
/*
Model_Name = > Name of the model that have GetContentById().
Assume that Model_Name => Content_Loader
*/
//$this->load->model(Model_Name);
$this->load->model('Content_Loader');
$data = $this->Content_Loader->GetContentById($content_id);
}
echo json_encode ($data);
}
}