− 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 jApp::coord()->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;