I will explain more, because it might not be clear enough
I want to set the ajax url to: http://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
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
header together with their ajax request. If yours doesn’t you could easily make it to send it using something like this:
Then in PHP you could check if request was made using Ajax by using this code block:
EDIT:
As Elzo Valugi mentioned, don’t forget to use correct content type when outputing JSON in your action by using:
before sending any JSON or else you might have some problems with some JavaScript libraries.
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.