search
Homephp教程php手册Zend Framework动作助手Json用法实例分析

本文实例讲述了Zend Framework动作助手Json用法。分享给大家供大家参考,具体如下: Json的使用较简单,以下是文档给出的用法: Json 用来解码和发送 JSON 响应; 当处理期望数据表响应的 AJAX 请求,JSON 响应迅速变成选择的响应。 JSON 可以立即在客户端被

本文实例讲述了Zend Framework动作助手Json用法。分享给大家供大家参考,具体如下:

Json的使用较简单,以下是文档给出的用法:

Json 用来解码和发送 JSON 响应;

当处理期望数据表响应的 AJAX 请求,JSON 响应迅速变成选择的响应。

JSON 可以立即在客户端被解析,从而快速执行。

JSON 动作助手完成以下任务:

如果布局是打开(enabled)的,则关闭(disable)它。

如果视图解析器(ViewRenderer)是打开的,则关闭它。

设置 'Content-Type' 响应头为 'application/json'。

缺省地,不需要等待动作执行完成,立即返回响应。

用法很简单:或者把它作为助手代理的方法来调用,或者调用 encodeJson() 和 sendJson() 方法的其中之一:

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
    // do some processing...
    // Send the JSON response:
    $this->_helper->json($data);
    // or...
    $this->_helper->json->sendJson($data);
    // or retrieve the json:
    $json = $this->_helper->json->encodeJson($data);
  }
}

Note: 保持布局 (Keeping Layouts)

如果你为 JSON 响应有分离的布局 - 也许把 JSON 封装到一些上下文 - 在 JSON 助手的每个方法接受第二个可选的参数:打开或关闭布局的 flag ,传递一个布尔 true 值将使布局保持打开:

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
    // Retrieve the json, keeping layouts:
    $json = $this->_helper->json->encodeJson($data, true);
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function