tags:

views:

150

answers:

4

Hello,

I will explain more, because it might not be clear enough

I want to set the ajax url to: www.domain/controller/method I already have everything in place for this with normal php.

But if I have to do this with ajax, I am not getting the intended result. The result should be some json variable being echo'd back to me. It is not entirely clear to me why.

In firebug I can see that the requestheader is not the same as the responseheader. I see the layout view in the response. I do not know yet how to bypass that. The basecontroller creates the view for the layout, but I have not extended the ajaxcontroller with the basecontroller?? For now I am running it threw another script that I call, but I would like it more if I could do it by the first method.

Has anyone some suggestions, please ?

EDIT

It seems after the comments below I need to provide some logic to disable the layout?

first attempt:

class testController extends baseController implements IController
{

    public function testit()
    {
    $this->disableLayout = TRUE;
    $check='testit';
    $data =array();
    header('Content-type: application/json');

      $output = array(
      "check" => $check,
      "user" => $data
      );

      $this->content = json_encode($output);

      exit(0); // Stop script.

    }
}

thanks, Richard

+1  A: 

Depending on your framework in the view you should return the view with json headers and without other information from the layout model.

header('Content-type: application/json');

also depending on how do you parse the result of your AJAX request you may have to set the response type to 'json'.

What I do in Zend Framework when working with Ajax requests and JSON is

$this->_helper->layout->disableLayout();

<?php echo $this->json($this->data) ?>

The json helper will add the headers.

Elzo Valugi
thanks, I edited my question.It is the layout that I see in the response.The ajax is working correct.I do not know how to not set the other header.
Richard
I added also some examples from my ZF experience.
Elzo Valugi
Thanks @Elzo, I have also come to the conclusion that I have to disable layout somehow in my little framework.
Richard
+4  A: 

I'm making a guess here, but I think you are trying to serve different content from same action depending how it was requested.

To detect weather the page was requested by Ajax you could use specific header sent by browser.

Usually most recent JavaScript libraries send

X-Requested-With:XMLHttpRequest

header together with their ajax request. If yours doesn't you could easily make it to send it using something like this:

//Just example using raw XMLHttpRequest
var request = new XMLHttpRequest();     
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
//If you use Javascript library, see the documentation how to set custom request headers.
//But as I said most modern libraries already send X-Requested-With

Then in PHP you could check if request was made using Ajax by using this code block:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
           strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
   //Send JSON.
} else {
   //Send HTML.
}

EDIT: As Elzo Valugi mentioned, don't forget to use correct content type when outputing JSON in your action by using:

header('Content-type: application/json');

before sending any JSON or else you might have some problems with some JavaScript libraries.


http://i47.tinypic.com/o5we91.jpg

This is more or less how the request + response headers should look like. Note that I'm actually sending some unneeded ones here like Prototype version, etc.

Maiku Mori
Maybe tell us which php framework you're using. Some of them have their own custom functions to send out headers. But it's seems that the headers is just a minor problem for you. As far as I can understand you're not getting the right content either (you get full page instead of JSON). That means that you have error in the detection or you're sending out layout + JSON. There is a way to disable layouts for some actions, maybe that's what you're looking for. For example if you're using Zend PHP Framework then the function to disable layout is: $this->_helper->layout->disableLayout();
Maiku Mori
Well, not entirely. It is a different controller and different method. But I guess the layout is causing to send different headers. I don't know why or where the layout header is send?The latter is my problem. The headers will look like look yours if I am running it from a single script and not threw a controller. So have to find out why the layout header is send.
Richard
@Maiku it is from http://www.phpro.org/, I is the advanced version or the WEB2BB framework
Richard
+1  A: 

I usually create a separate controller to handle Ajax requests. For example, /ajax/getusers/?page=2 may return the HTML for page two of a paginated users grid. I like this approach because it reminds me to keep my application logic and UI building logic separate.

Alternatively, you could just pass in a querystring with each ajax request such as: www.domain.com/controller/method/?ajax=1 and then hide your layout components when this is present.

Kevin
thanks,I have made a seperate controller the same way as the other controllers in the fr-work. The only issue here is how to hide the layout stuff.
Richard
Ah, I didn't see what framework you're using. In CodeIgnitor, each method must explicitly say which View to load and I thought most php frameworks worked like this.
Kevin
A: 

You need to override the destructor of the basecontroller class. Copy the destructor in your controllerclass and modify the template file. Maybe you use an empty file. There is no hack required

Bandlow