|^ Web services||JSON »|
Although Ajax means an XML response (X in AJAX), it is commonly used with other formats.
Depending on the client waiting either
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
jResponseHtmlFragment(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
<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.
You must then include those additional scripts and stylesheets, manually, in the response which initiated the AJAX call.