− Table of content
Although Ajax means an XML response (X in AJAX), it is commonly used with other formats.
Depending on the client waiting either responseText
or responseXml
of an XmlHttpRequest
object, you should use differents jelix response types.
- for any generic XML content, you will use jResponseXml
- for json encoded content, you will use jResponseJson
- for html fragments, beware you must NOT use
jResponseHtml
, butjResponseHtmlFragment
(see example below). - Or, in other cases, you would use jResponseText.
In your controller, you can use $GLOBALS['gJCoord']->request->isAjax()
to verify that the request is a true AJAX request (eg, made with XMLHttpRequest).
Sending HTML fragments ¶
You can't use jResponseHtml
as it always generates a full well-formed HTML with <html>
,<head>
and <body>
... This is not what we want within an AJAX transaction.
jResponseHtmlFragment
will serve your needs here:
$resp = $this->getResponse('htmlfragment');
$myvariable = ...
// html to send
$resp->addContent( '<p>'.$myvariable.'</p>' );
// or with a template:
$resp->tplname ='myapp~tpl_for_ajax';
$resp->tpl->assign('variable', $myvariable);
return $resp;
Note about the use of jforms in an ajax response ¶
If you want to include a jforms form inside an ajax response, you have to remember the following.
jForms builders try to include additionnal CSS stylesheets and javascript files in the response. Unfortunately, AJAX responses does not support such features.
You must then include those additional scripts and stylesheets, manually, in the response which initiated the AJAX call.