从PHP控制器运行AJAX
很抱歉,听起来让人困惑,但我会尽可能用最好的方式解释 在控制器中,我有一个函数搜索从PHP控制器运行AJAX,php,ajax,codeigniter,Php,Ajax,Codeigniter,很抱歉,听起来让人困惑,但我会尽可能用最好的方式解释 在控制器中,我有一个函数搜索 public function search(){ /* I run my logics and get few URL from where I need to fetch further data the urls are saved in the URL array $urls[0] = "http://url1.com/search1"; $urls[
public function search(){
/*
I run my logics and get few URL from
where I need to fetch further data
the urls are saved in the URL array
$urls[0] = "http://url1.com/search1";
$urls[1] = "http://url2.com/search2";
I then set this in data variable and send it to view
so that It can be run in AJAX
I tired running get_file_contents but it executes
in series one after the other URL.
If there are 10 URL (5 secs per URL) the over all processing time
increases drastically
*/
$data["urls"] = $urls;
$resp = $this->load->view('ajaxer',$data,TRUE);
/* based on the $resp i need to run further business logic's */
}
现在$resp实际上只给了我HTML代码。它没有执行HTML,因此没有运行ajax
任何关于如何执行此操作的想法都将非常有用
问候,,
Amit您的代码完全正常。但是您的javascript没有得到任何响应数据(只有标题),因为您没有返回任何输出 如果您想“执行您的HTML”,您需要将视图中的行更改为:
$this->load->view('ajaxer',$data);
或者这个:
$resp = $this->load->view('ajaxer',$data,TRUE);
echo $resp;
您忘记在控制器中回显输出。除此之外,您还需要对函数进行一些小的修改
public function search(){
/*
I run my logics and get few URL from
where I need to fetch further data
the urls are saved in the URL array
$urls[0] = "http://url1.com/search1";
$urls[1] = "http://url2.com/search2";
I then set this in data variable and send it to view
so that It can be run in AJAX
I tired running get_file_contents but it executes
in series one after the other URL.
If there are 10 URL (5 secs per URL) the over all processing time
increases drastically
*/
// You need to check either request came from Ajax request or not. If not it will echo passed string. It prevents to access this function besides Ajax request
if (!$this->input->is_ajax_request()) {
echo "Ajax Requests allowed.";
die;
}
$data["urls"] = $urls;
$resp = $this->load->view('ajaxer',$data,TRUE);
// Standard way to set response for output in json format.
// @param status will help to check all things goes correct or not. if not please pass false on the basis or your feature's requirement
$this->output->set_output(json_encode(array('status'=>true,'response'=>$resp)));
// Standard way to get output set above step.
$string = $this->output->get_output();
echo $string;
exit();
/* based on the $resp i need to run further business logic's */
}
更新的代码在这里。希望您能找到答案问题可能是因为您没有在ajax选项中包含
success
项。选项success
是接收控制器输出的功能。在该函数中,您可以操作html以使用返回的数据。添加进行ajax调用的javascript代码和要更新的视图的html。@DFriend我有一个成功选项。如果成功,我将更新特定的div html。但是,div是空的,我在$resp$resp
中得到了完整的HTML代码,其中包含HTML,因为这是load->view('someview')的预期目的代码>-返回文件“someview”中的内容(通常为html)。清楚地解释你想要达到的目标会非常有帮助。例如,您计划如何处理搜索URL?