Slim PHP returning JSON

So basically I have my basic application making an ajax GET call for my slim php application. It expects a JSON dataType in return.

$.ajax({ url: './myroute', type: 'GET', dataType: "json", data: { username: username, password: password }, success: function(data) {}, error: function(data) {} }); 

In my thin file I have:

 $app->get('/myroute', function() use ($app) { // all the good stuff here (getting the data from the db and all that) $dataArray = array('id' => $id, 'somethingElse' => $somethingElse); $response = $app->response(); $response['Content-Type'] = 'application/json'; $response->body(json_encode($dataArray)); }); // Tried with these in the above GET request as well: // $app->contentType('application/json'); // echo json_encode($dataArray); 

While my request passes correctly (200) and I get the JSON data correctly, the error is due to the fact that it also returns the full data of the index.php page (which my javascript dataType: "json" does not allow, which causes an error)

I decided to set the content type to "application / json" to solve this problem, but it still returns the full content of the page as well as the json data.

Edit for reference

I used it so that Slim displays my html as:

 $app->get('/', function () use ($app) { // would just have my main page files in home.php instead of index.php $app-render('home.php'); }); 

So there was no html page data returned from index.php. But the pushState way is that I have to have javascript scripts running on index.php, otherwise my pages will not load properly since they are requested, the scripts will not delegate where the route should go.

Any help is appreciated!

Thank you SO!

+7
source share
4 answers

Not familiar with the subtle framework. It looks interesting. It seems that the code continues to work after displaying json. Perhaps try exit; enable php application after responding to your json?

 $app->get('/myroute', function() use ($app) { // all the good stuff here (getting the data from the db and all that) $dataArray = array('id' => $id, 'somethingElse' => $somethingElse); $response = $app->response(); $response['Content-Type'] = 'application/json'; $response->body(json_encode($dataArray)); exit(); }); $app->run(); 

Hope this helps

+10
source

Try changing body () to write (),
I think this will work: $ response-> write (json_encode ($ dataArray));

+5
source

Thin JSON return documentation mentions what you are looking for.

Again from the documentation, each route callback takes three arguments:

Requirements
The first argument is the Psr \ Http \ Message \ ServerRequestInterface object, which represents the current HTTP request.
answer
The second argument is the Psr \ Http \ Message \ ResponseInterface object, which represents the current HTTP response.
Arguments
The third argument is an associative array that contains values ​​for the current routes with placeholders.

Quote from the same page:

If you use the Closure instance as a route callback, the closing state is bound to the Container instance. This means that you will have access to the DI container instance inside Closure using this keyword $.

Therefore, when defining a route callback, you do not need uese ($app) .

Also, as mentioned in the documentation

Ultimately, each Slim application route MUST return a PSR 7 response object.

In conclusion, this should do what you expect:

 $app->get('/myroute', function($request, $response, $args) { // all the good stuff here (getting the data from the db and all that) $dataArray = array('id' => $id, 'somethingElse' => $somethingElse); return $response->withJson($dataArray); }); 
+1
source

exit(); no longer works for me, I don’t know why. Here's how I solved it:

 $response = $app->response(); $response['Content-Type'] = 'application/json'; $response->body(json_encode($body)); return $response; 
0
source

All Articles