Thursday, 1 April 2021

Symfony v2.8 Response( $message ) method 'returning' more than expected

My goal is to return a message to my web-page, where it is to be displayed, but instead of just the message, the value returned contains the message and html code for an error page!

I am using the following code to return a message result to an ajax call:

Server-Side PHP code that is called by an Ajax call ...

$message = 'Some message to be displayed.';

// Return to the page and display the message ...

return new Response( $message );  // Inspecting $message shows only
                                  // 'Some message to be displayed.'

Client-Side JavaScript code that calls the above code and receives the response:

$.ajax( { // Send form data to the same page that the page came from
          type:        'POST',
          data:        data,
          cache:       false,
          async:       true,
          contentType: false,
          enctype:     'multipart/form-data',
          processData: false
        } )
.done( function( data, textStatus, jqXHR ) { ... } // Inspecting data
                                                    // shows more than
                                                    // 'Some ...
.fail( function( jqXHR, textStatus, errorThrown ) { ... }

When breakpoints are set at the return new Response( $message ); and first executable line in the .done(...) function, the PHP script execution gets to the breakpoint at the return new Response( $message ); statement without any errors and I can see that the parameter's value is 'Some message to be displayed.', as expected, but when paused at the .done() function's first line, the data parameter's value is: Some message to be 'displayed.<!DOCTYPE html><html>...</html>!

Where, if the html code is placed in a file and loaded into the browser it looks somewhat like this:

Whoops, looks like something went wrong.
1/1 FatalErrorException in debug-eval(1) : eval()'d code line 1:
Parse Error: syntax error, unexpected ')'
in debug-eval(1) : eval()'d code line 1

An obvious error message, but my code isn't using any eval() statements that could fail and the execution got to the Response statement without any errors. Is this a bug in the Symfony Response statement method/function? If not, how can I find out what is really happening, so I can fix it?

Follow-Up

Changing the server-side code to encode a json string and then decoding it on the client-side doesn't help.

Server-Side PHP code:

// Return to the login page and display the message ...

// $message'value => '{ "message":"' . $message . '"}';

$message = json_encode( [ 'message' => $message ] ); 
return new Response( $message );

Client-Side Javascript:

    ***unchaged***

In this case I'm still inspecting the data parameter, but it now now contain the json encoded message AND the html error page code. Feeding this to the JSON.parse( data ); statement to convert the json data to an javaScript object causes the javaScript to freeze. This isn't a json issue, the problem of the html code appended to the end of my message still is happening either way.

Thank you



from Symfony v2.8 Response( $message ) method 'returning' more than expected

No comments:

Post a Comment