1

I am sorry to sound confusing but I will try to explain in the best way possible.

In the controller I have a function search

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 */

}

Now the $resp is actually giving me only the HTML code. It is not executing the HTML and hence the ajax is not run.

Any thoughts on how to execute this will be really helpful.

Regards, Amit

3
  • The problem is probably because you don't include a success item in the ajax options. The option success is a function that receives the output of the controller. In that function is where you manipulate the html to use the returned data. Add the javascript code where you make the ajax call and the html of the view to be updated. Commented Apr 15, 2017 at 18:53
  • @DFriend I do have a success option. If its a success, i update a particular div html. However, the div is empty and I get the full HTML code in the $resp Commented Apr 15, 2017 at 19:01
  • $resp contains html because that is the intended purpose of load->view('someview'); - to return the contents (typically html) in the file "someview". A clear explanation of what you are trying to achieve would be very helpful. For instance, what do you plan to do with the search URLs? Commented Apr 15, 2017 at 20:00

2 Answers 2

1

Your code is absolutelly ok. But your javascript is not getting any response data (only headers), because you are not returning any output.

If you want to "execute your HTML" you need to change the line with view to this:

$this->load->view('ajaxer',$data);

or this:

$resp = $this->load->view('ajaxer',$data,TRUE);
echo $resp;
Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to echo output in the controller. Apart from this you need few minor modification in your function.

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 */

}

Updated code is here. Hope you find your answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.