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) {
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!
Devin
source share