Hi,
I want to query my database using AJAX with JQUERY. This is the Javascript function I wrote to initiate the call:
var requestResponders = function(e) {
e.preventDefault();
getResponders(latitude, longitude, function(data) {
console.log(data);
});
}
And the AJAX request:
function getResponders(latitude, longitude, callback) {
$.ajax({
type: 'POST',
url: '../searchResponders',
data: {
latitude: latitude,
longitude: longitude
},
success: function(responders) {
console.log(responders);
callback(responders);
},
error: function(e) {
console.log("Error");
}
})
};
Therefore I set in my routes file:
Route::post('searchResponders', 'RespondersController@getAvailableResponders');
And the PHP to MYSQL Query:
public function searchResponders($geoInformation) {
$lat = $geoInformation['latitude'];
$lng = $geoInformation['longitude'];
$results = DB::select(DB::raw("SELECT id, (6371 * acos(cos(radians(:var1)) * cos(radians(last_lat)) * cos(radians(last_lng)-radians(:var2))+sin(radians(:var3))*sin(radians(last_lat)))) AS distance FROM responders HAVING distance < 2 ORDER BY distance LIMIT 0, 3"), array('var1' => $lat, 'var2' => $lng, 'var3' => $lat));
echo json_encode($results);
}
Now something strange happens. If I use POSTMAN to check the POST call, I receive the database output perfectly fine. So I can be sure already, that the SQL part and the routing works.
But when I check the AJAX call to the same function, it returns and empty return value. So usually I would think, this means that something is being returned before the PHP DB call finished. But this should be excluded by using the deferred object.
When I log the output in the console, I see that the code in the "done" section is being called, but the result return value is empty.
Why is that?