2016-04-25 13 views
0

私が持っているようなAjax呼び出し、以下:Zendのコントローラから値を渡すと、JS応答でそれをフェッチ

$.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 
} 

アクションはないのニーズ基本的に何かを返すためには、ログインデータがOKかどうかをチェックするだけです。しかし、私がここで行う必要があるのは、応答がJavascriptによって返されたときに、JSスクリプトファイル内で処理する必要のあるデータを何とか取得するというアクションで何らかのことを言うことです。 Zendフレームワークでこれをどうすればできますか?誰かが私を助けてくれますか?

+0

みんな、誰? – perkes456

答えて

0

最初に、応答でレイアウトを禁止する必要があります。

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アクションが含まれています

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応答を構築:そのためには、あなたの方法Module::onBootstap()に次のコードを入れてモジュール全体:

$result->setTerminal(true); 
+0

コントローラからビューにIDを転送するための簡単な方法はありますか?多分私はそれをセッションに保存して、.phtmlファイルから何とかアクセスできますか? – perkes456

+0

.phtmlファイルではJSレスポンスは得られません。 –

関連する問題