2015-04-06 175 views
0

嘗試訪問由ajax發送給php的JSON變量時遇到問題。 EnvíoLA varible 「郵報」:請求的JSON解析失敗(ajax)+ zf2

<script> 
$("#recuperar").click(function() { 
    var url1 = "<?php echo $this->url('login',array('controller'=>'Login', 'action'=>'recuperar')) ?>"; 
    var consulta = $("#correo").val(); 
    $.ajax({ 

     type: "POST", 
     url: url1, 
     data: {"correo": consulta}, 

     dataType: "json", 

     beforeSend: function() { 

     }, 
     error: function (jqXHR, exception) { 

      if (jqXHR.status === 0) { 
       alert('Not connect.\n Verify Network.'); 
      } else if (jqXHR.status == 404) { 
       alert('Requested page not found. [404]'); 
      } else if (jqXHR.status == 500) { 
       alert('Internal Server Error [500].'); 
      } else if (exception === 'parsererror') { 
       alert('Requested JSON parse failed.'); 
      } else if (exception === 'timeout') { 
       alert('Time out error.'); 
      } else if (exception === 'abort') { 
       alert('Ajax request aborted.'); 
      } else { 
       alert('Uncaught Error.\n' + jqXHR.responseText); 
      } 
     }, 
     success: function (data) { 

      $("#mostrar").empty(data); 
      $("#mostrar").append(data['error']); 
     } 
    }); 
}); 

我的控制器:

public function recuperarAction() { 
    $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); 

    if ($this->request->isXmlHttpRequest()) { 

    $correo = $_REQUEST['correo']; 
    print_r($correo); 

當我嘗試print_r($email)返回我這個錯誤:請求JSON解析失敗。

這也適用於如果到print_r(some_value),如果我嘗試打印任何數據會返回相同的錯誤。

感謝幫助,對不起我的英語是純正的谷歌翻譯

回答

0

首先,你應該你的數據類型設置爲application/jsonjson您目前擁有的json數據不是正確的Content-Type聲明。

如果不解決您的問題,嘗試用json驗證例如像json lint驗證您json數據。

+0

感謝您的迴應,您可能會向我解釋如何爲Json數據配置應用程序。非常感謝,真的是ZF2和Doctrine2的新手。 – 2015-04-07 16:24:50