从 Zend 控制器传递值并在 JS 响应中获取它
Passing value from Zend controller and fetching it in JS response
我有一个Ajax调用,如下所示:
$.post("/user/signindo",{'username':username,"password":password},function(data)
{
// this is what I would like to be able to have in my data object, to be able to access these properties and display them once response is there
alert(data.id);
alert(data.username);
alert(data.firstname);
}
这是我的Zend控制器操作:
public function signindoAction()
{
// doing something here with the values passed from the view
}
该操作基本上不需要返回任何内容,因为它只是检查登录数据是否正常。但是,我在这里需要做的是以某种方式在操作中说,当 Javascript 返回响应时,它会以某种方式获取我需要在 JS 脚本文件中处理的数据。如何使用 Zend 框架执行此操作?有人可以帮我吗?
首先,您需要禁止布局作为响应。为此,请将以下代码放入您的方法Module::onBootstap()
中:
public function onBootstrap(MvcEvent $e)
{
$sharedEvents = $e->getApplication()->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
$result = $e->getResult();
if ($result instanceof 'Zend'View'Model'ViewModel) {
// ban the layout only for ajax
$result->setTerminal($e->getRequest()->isXmlHttpRequest());
// To ban the layout for all requests
// set true : $result->setTerminal(true);
} else {
throw new 'Exception('SbmAjax'Module::onBootstap() the result isn't a 'Zend'View'Model'ViewModel');
}
});
}
然后在控制器中构建 html 响应,如下所示:
public function signindoAction()
{
// doing something here with the values passed from the view
return $this->getResponse()->setContent(Json::encode(array(
'data' => ...something,
'success' => 1
)));
// to handle errors return response with 'success' => 0
}
就个人而言,我将所有ajax操作包含在特定模块中,并作为整个模块的设置:
$result->setTerminal(true);
相关文章:
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- 使Intro.js工具提示响应
- angular.js我如何设置数据什么是响应值
- Ember.js:将Em.$.getJSON转换为promise并将响应绑定到控制器上下文的正确方法
- D3.js:将一个有响应的饼图放在其父分区的中心
- 使用Backbone.js访问JSON响应的部分
- 引导:响应式设计-当窗口大小从980px调整到979px时执行JS
- 为什么新js Fetch标准禁止响应头名称为'设置Cookie 1/2'
- JS-通用解析JSON响应
- 分析angular.js中的JSONP$http.json()响应
- Rails 4远程true和响应js
- 为什么我的rails应用程序在生产环境中没有正确响应.js格式
- 如何使用响应式iframes使iframe响应.js
- 如何在没有 Web 服务器的情况下做出响应.js工作
- 响应.js将首选媒体查询而不是稍后定义的全局选择器
- IE8 支持使用响应.js进行媒体查询
- ie8响应js'未定义'为null或不是对象
- mobilesafari 5在下一次行动之前没有响应js事件
- 命令按钮不响应JS函数
- 如何在向第三方 API 节点发出多个请求后发送响应.js.