只是想知道为什么这不起作用。
当在单独的方法中放置JSON返回时,我在控制器中收到以下错误:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? (500 Internal Server Error)
设置如下:
控制器
namespace UsedBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
...
use Symfony\Component\HttpFoundation\JsonResponse;
class AccountController extends Controller
{
private $status;
private $message;
private $data;
/**
* @Route("/mon-compte", name="account_page")
*/
public function showAccount(Request $request){
$factory = $this->get('security.encoder_factory');
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
throw $this->createAccessDeniedException();
}
$user = $this->getUser();
$session = $request->getSession();
$email = $session->get('email');
$em = $this->getDoctrine('doctrine')->getManager('used');
$this->user_info = $em->getRepository('UsedBundle:User')
->UserAccountInfoAction( $email );
$form = $this->createForm(UserType::class, $user);
if ($request->isMethod('POST')) {
if($_POST['action'] == 'update_user'){
$this->updateProfile($request, $user, $form, $em);
}elseif($_POST['action'] == 'delete_user'){
$this->deleteUser( $user, $em );
}elseif($_POST['action'] == 'update_password'){
$this->updatePassword( $user, $em, $factory);
}
// \Doctrine\Common\Util\Debug::dump($this->data);
$this->returnJson();//***** this is generating the error*****
}else{
// populate change profile form
return $this->render('account/account.html.twig', [
'user_info' => $this->user_info,
'form' => $form->createView(),
]);
}
}
然后,进一步在该类上我有returnJson()方法:
public function returnJson(){
return new JsonResponse(array(
'status' => $this->status,
'message' => $this->message,
'data' => $this->data,
)
);
}
如果我将该代码替换为showAccount()上的$ this-> returnJson(),它将正常工作。
为什么退货不能作为单独的方法放置?或者我错过了什么。
由于
答案 0 :(得分:3)
您的success: function (json) {
$('#hasil').append("<div class='list-group-item row'>");
for (var i = 0; i < json.data.length; i++)
{
$('#hasil').append("<div class='col-md-4' id='col"+i+"'>"+json.data[i]+"</div>");
}
$('#hasil').append("</div>");
函数会将returnJson
返回JsonResponse
函数,而不是showAccount
函数。
这应该有效:
return $this->returnJson();