CodeIgniter Forums
Use action from controller to return data to AJAX or load views with content? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Use action from controller to return data to AJAX or load views with content? (/showthread.php?tid=56585)



Use action from controller to return data to AJAX or load views with content? - El Forum - 01-04-2013

[eluser]Unknown[/eluser]
Hi all, I'm creating a web app, sometimes I need to load a "view" to view client details, sometimes I need JSON data only.

How can I do it?

Need I create two actions? (One to return JSON data and one to load the "view").

Tks :-)


Use action from controller to return data to AJAX or load views with content? - El Forum - 01-09-2013

[eluser]Fationably Late[/eluser]
You could do something like this:

Code:
public function delete_image($imageID = NULL,$ajax = NULL)
  {
    $result = Media::removeImage($imageID);
    if($ajax){
      echo json_encode($result);
      exit; // <- note the exit here!
    }
    return $result;
  
  }

and append a segment to the end of your url in the ajax call.
OR
You could pass an 'unknown' variable in the $_POST array of your ajax call and check for it there.

Code:
public function delete_image($imageID = NULL)
  {
    $result = Media::removeImage($imageID);
    if( array_key_exists($this->input->post('ajax')) ){ // or however you want to check for it
      echo json_encode($result);
      exit; // <- note the exit here!
    }
    return $result;
  
  }



Use action from controller to return data to AJAX or load views with content? - El Forum - 01-09-2013

[eluser]Aken[/eluser]
Or you could use $this->input->is_ajax_request().


Use action from controller to return data to AJAX or load views with content? - El Forum - 01-09-2013

[eluser]Fationably Late[/eluser]
[quote author="Aken" date="1357772881"]Or you could use $this->input->is_ajax_request().[/quote]

That's true, I forgot about that. Good call... I was thinking though, if they're using jQuery for their ajax methods, it doesn't automatically set the header to detect such a thing (HTTP_X_REQUESTED_WITH), as far I'm aware. Please correct me I'm wrong here.

Plus I think there may be some added 'security' by including variables or segments (especially if they are hashed) in the request. But I'm getting off topic.


Use action from controller to return data to AJAX or load views with content? - El Forum - 01-09-2013

[eluser]Aken[/eluser]
Most Javascript frameworks implement that header as a psuedo-standard. It should be fairly reliable, but obviously testing and verification are important for developers. You could even use your own custom header name/value pair to "validate" the request (this is not enough for actual validation, but it works the same as is_ajax_request()).