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 !

Goku
  • 2,157
  • 2
  • 16
  • 31
ZeSoft
  • 305
  • 2
  • 4
  • 13
  • It probably won't change anything, but you should return `$response` in action instead of calling `send()` method directly. – Jakub Matczak Nov 04 '16 at 16:25
  • Are you sure it's a PHP related error? I have seen this error on Javascript side. – felipsmartins Nov 04 '16 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, – ZeSoft Nov 04 '16 at 16:34
  • felipsmartins it's php related – ZeSoft Nov 04 '16 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. – Alvin Bunk Nov 04 '16 at 16:47
  • what happens if you just put an empty array into the JsonResponse?? does it result in the same error? – Nickolaus Nov 04 '16 at 16:57
  • Alvin i did var dump the array is formated correctly {'message':'the contnt of the message','success':true} this is not the problem because when i remove send method it works – ZeSoft Nov 04 '16 at 20:04
  • Nickolaus even if the array is empty, the jsonResponse cannot use send function that's what i concluded – ZeSoft Nov 04 '16 at 20:06
  • At least with sf v3.1, I can't reproduce the error. Meaning the cookie get's cleared when simply returning the response. Prematurely *sending the response* seems wrong anyway and will probably produce to much output sent to the browser. Instead of working around the *send* method clarify why you think the cookie doesn't get removed. – Yoshi Nov 07 '16 at 08:53
  • Yoshi the error occures when i call jsonresponse->send from an ajax request, and i've tried returning the response without send but it only works with send and many websites recommend calling send in order for the cookie to get deleted – ZeSoft Nov 07 '16 at 11:30

1 Answers1

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.

Kaveh Yzd
  • 119
  • 1
  • 12
  • if i use return without the function send the deleteCookie doesn't work – ZeSoft Nov 04 '16 at 20:07
  • actually it works in controllers but not in authentification handler and especialliy with the remember_me cookie – ZeSoft Nov 07 '16 at 14:40