如何以自定义方式在Zend Framework应用程序中实现jquery.
>附加jquery.js确定
>附加脚本确定
>将POST数据发送到控制器ok
>处理POST数据确定
>发送’AjaxContext’回复客户端现在好了(谢谢)
我第一次使用jquery,我做错了什么?
最佳答案
在早期,让Zend在没有完整布局的情况下响应ajax请求的最佳做法是检查通过请求头提供的变量.根据documentation许多客户端库,包括jQuery,Prototype,Yahoo UI,MockiKit都发送正确的标题为此工作.
if($this->_request->isXmlHttpRequest())
{
//The request was made with via ajax
}
然而,现代实践,以及您可能正在寻找的东西,现在使用两个新助手中的一个:
> ContextSwitcher
> AjaxContent
这使得这个过程更加优雅.
class CommentController extends Zend_Controller_Action
{
public function init()
{
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('view', 'html')
->initContext();
}
public function viewAction()
{
// Pull a single comment to view.
// When AjaxContext detected, uses the comment/view.ajax.phtml
// view script.
}
请注意:这种现代方法要求您请求格式以便触发上下文.它在文档中并不是很明显,当你最终在浏览器中得到奇怪的结果时会有点混乱.
/url/path?format=html
希望有一个我们可以发现的解决方法.有关更多详细信息,请查看full documentation.
相关文章