Can';t在zend framework 3中使用ajax发送
我正试图发送一个ajax来调用GrupoUsuarioController中的函数“AlterapermissionAction” js代码:Can';t在zend framework 3中使用ajax发送,ajax,zend-framework3,Ajax,Zend Framework3,我正试图发送一个ajax来调用GrupoUsuarioController中的函数“AlterapermissionAction” js代码: $.ajax({ type: 'POST', url: '<?= $this->url( 'grupo-usuario-insumo', [ 'action' => 'alterarPermissoes' ] ) ?>', data: dados, dataType: 'json', suc
$.ajax({
type: 'POST',
url: '<?= $this->url( 'grupo-usuario-insumo', [ 'action' => 'alterarPermissoes' ] ) ?>',
data: dados,
dataType: 'json',
success: function( r ) {
console.log( r );
}
});
$.ajax({
键入:“POST”,
url:“”,
数据:护墙板,
数据类型:“json”,
成功:功能(r){
控制台日志(r);
}
});
当我这样做时,zf3尝试呈现一个页面并将该页面作为响应发送给我
我应该怎么做才能使zf3停止尝试渲染页面?在控制器中,AlterarPermissionAction必须以以下内容结束:
return $this->getResponse()->setContent(
Json::encode(
[
[your response]
]));
但要不应用布局,必须在Module.php
中指定如下内容:
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) {
$result->setTerminal($e->getRequest()->isXmlHttpRequest());
} else {
throw new \Exception([your message]);
}
});
}
如果需要数据响应,应确保通过
ViewJsonStrategy
使用不同的ViewHelper
。将以下内容添加到模块的配置中
'view_manager' => [
'strategies' => [
'ViewJsonStrategy', // This is an alias, so add as string
],
],
接下来,您的控制器中有一个操作,它必须知道它必须返回Json。您可以使用JsonModel
-下面的示例代码(来自链接):
如果希望能够有条件地使用此选项,例如在不刷新页面的情况下使用动态表单响应,则需要检查请求是否为XmlHttpRequest
,如下所示:
namespace Application\Controller;
use Zend\Mvc\Controller\ActionController;
use Zend\View\Model\JsonModel;
class IndexController extends ActionController
{
public function getJsonAction()
{
if ($this->getRequest()->isXmlHttpRequest()) {
return new JsonModel([
'status' => 'SUCCESS',
'message'=>'Here is your data',
'data' => [
'full_name' => 'John Doe',
'address' => '51 Middle st.'
]
]);
}
return [
'normal' => 'response',
];
}
}
你的控制器动作看起来怎么样?谢谢你的回答!我做了一个“快速修复”,将一个骰子函数放在那里工作,但现在我要更改它
namespace Application\Controller;
use Zend\Mvc\Controller\ActionController;
use Zend\View\Model\JsonModel;
class IndexController extends ActionController
{
public function getJsonAction()
{
if ($this->getRequest()->isXmlHttpRequest()) {
return new JsonModel([
'status' => 'SUCCESS',
'message'=>'Here is your data',
'data' => [
'full_name' => 'John Doe',
'address' => '51 Middle st.'
]
]);
}
return [
'normal' => 'response',
];
}
}