Welcome Guest, Not a member yet? Register   Sign In
response->setJSON() and response->setStatusCode()
#1

(This post was last modified: 08-21-2023, 02:16 AM by joho.)

Does the response->setJSON() function do different things based on what response->setStatusCode() has been given previously?

The reason I ask is that if I issue a response->setStatusCode(200, "Some messge"), the JS code that picks up the response thinks that my returned data is in JSON format. If I, however, do a response->setStatusCode(204, "No data"), the JS code that picks up the response thinks the data is not in JSON format. That's only difference between the succesful call and the call that fails.

(08-21-2023, 01:43 AM)joho Wrote: Does the response->setJSON() function do different things based on what response->setStatusCode() has been given previously?

The reason I ask is that if I issue a response->setStatusCode(200, "Some messge"), the JS code that picks up the response thinks that my returned data is in JSON format. If I, however, do a response->setStatusCode(204, "No data"), the JS code that picks up the response thinks the data is not in JSON format. That's only difference between the succesful call and the call that fails.

Even doing this won't affect the outcome, if I use status code 204:


Code:
                $message = 'No articles retrieved';
                if ( defined( 'APP_IS_DEVELOPMENT' ) ) {
                    error_log( basename(__FILE__) . '(' . __LINE__ . '): ' . $message );
                }
                $data = [
                    'success' => true,
                    'message' => $message,
                ];
                $this->response->setHeader( 'Cache-Control', 'no-cache' )
                              ->appendHeader( 'Cache-Control', 'must-revalidate' )
                              ->setStatusCode( 204, $message )
                              ->setContentType( 'application/json' )
                              ;
                return( $this->response->setJSON( $data ) );

-joho
Reply




Theme © iAndrew 2016 - Forum software by © MyBB