3

I am using the HttpFoundation in my small project: use \Symfony\Component\HttpFoundation\JsonResponse as JsonResponse;

Unfortunately all my responses (tried JsonResponse, Response and BinaryFileResponse) only return a blank page, no errors and the code gets executed normally, e.g.

/* Get Inputs */
if (!$data = filter_input(INPUT_GET, 'url', FILTER_VALIDATE_URL)) {
    return new JsonResponse(array(
        'result' => 'error',
        'message' => 'URL is invalid or missing'
    ));
}else{
     return new JsonResponse(array(
        'result' => 'success',
        'message' => 'FINE'
    ));

There are no errors in the logs either.

Any ideas how to approach the issue?

//UPDATE FOR CLARIFICATION

$json = new JsonResponse(array(
    'result' => 'error',
    'message' => 'Encrypt is invalid or missing'
));

echo $json;

returns HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json {"result":"error","message":"Encrypt is invalid or missing"}

but why does the return not work?

cn007b
  • 16,596
  • 7
  • 59
  • 74
PrimuS
  • 2,505
  • 6
  • 33
  • 66
  • what if you echo the response? Are you invoking the service via Ajax? – GuyT Dec 21 '15 at 08:44
  • No, it's a direct call and "echo" gets displayed fine – PrimuS Dec 21 '15 at 08:46
  • Even if you put the `JsonResponse` in a variable? – GuyT Dec 21 '15 at 08:52
  • is display_errors set to on? – Clay Dec 21 '15 at 08:57
  • display_errros is on. `$json = new Json Response...` and then `echo $json` returns `HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json {"result":"error","message":"Encrypt is invalid or missing"}` so there is seomthing wrong with the `return` statement? – PrimuS Dec 21 '15 at 09:00
  • Which framework? Apparently your application does not handle `Response` objects returned from the controller. – Alex Blex Dec 21 '15 at 09:05
  • Well, not using a full stack framework, but the Response I get with this: `use \Symfony\Component\HttpFoundation\JsonResponse as JsonResponse;` – PrimuS Dec 21 '15 at 09:22

2 Answers2

4

You're not using the full stack framework, so you need to be sure that your front controller or equivalent calls $response->send(); to deliver the response to the client.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
0

It's addition to answer:

$response = new JsonResponse(array(
    'result' => 'error',
    'message' => 'Encrypt is invalid or missing'
));
$response->send();
Community
  • 1
  • 1
cn007b
  • 16,596
  • 7
  • 59
  • 74