2

I am working on a symfony application, and I am trying to delete a remember me cookie, using the following code:

$response->headers->clearCookie($cookieName,'/');

I have to call the response->send() method for this to take effect, this works perfectly with a simple response, but when i try to use it with a jsonResponse, the function send() returns this error:

JSON.parse: unexpected non-whitespace character after JSON data

There is nothing wrong with my json data, even if i specify no data it seems that send function is just not working with a jsonResponse

Here is my code for the jsonResponse :

$array = array('message'=>'your account is disabled','success'=>false);
$response = new JsonResponse($array);
$response->send(); //this triggers the error

Your help will be appreciated !

10
  • It probably won't change anything, but you should return $response in action instead of calling send() method directly. Commented Nov 4, 2016 at 16:25
  • Are you sure it's a PHP related error? I have seen this error on Javascript side. Commented Nov 4, 2016 at 16:26
  • Jakub if i return the response it doesn't clear the cookie, so i have to use the send method which generate this error, and adding the return response wony change a thing, Commented Nov 4, 2016 at 16:34
  • felipsmartins it's php related Commented Nov 4, 2016 at 16:34
  • Can you try doing a var_dump or similar on $response before the send to see what it looks like? I'm wondering what the no-whitespace character might be. I may have seen something like this before. Update your post to show what it prints out. Commented Nov 4, 2016 at 16:47

1 Answer 1

1

your can use this code in controller

$array = array('message'=>'your account is disabled','success'=>false);
return new JsonResponse($array);

Send, in controller is not valid and you can use return in controller.

Sign up to request clarification or add additional context in comments.

2 Comments

if i use return without the function send the deleteCookie doesn't work
actually it works in controllers but not in authentification handler and especialliy with the remember_me cookie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.