Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sesc360's avatar

AJAX request to PHP Database query returns no value

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?

0 likes
10 replies
pmall's avatar

What if you do just

$.ajax({
    type: 'POST',
    url: '../searchResponders',
    data: {
        latitude: latitude,
        longitude: longitude
    },
    success: function(result) {
        console.log(result);
    },
})

Your js seems strange

sesc360's avatar

I tested, if the success block is being called, which is the case. but when I log the content of the response data in the ajax request itself, it is already empty. So when the ajax request is being made to the URL with POST and the data to be sent, it returns empty. But isn't this the same approach as make the POST request by POSTMAN for example? There a response is being made.

pmall's avatar

I tested, if the success block is being called, which is the case. but when I log the content of the response data in the ajax request itself, it is already empty.

You tried my code above ?

sesc360's avatar

yep. But there is an empty return value

jordiglezllinas's avatar

I had the same issue yesterday. After spend a few hours I decided to use GET request. Probably is not the best practice, but now it works.

sesc360's avatar

I changed that to GET but still no effect. This is so strange.... the direct call with POSTMAN works perfectly! And I use POST there as well.

sesc360's avatar

I thought about that first as well, but I already included that:

(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
        }
    });
})();
sesc360's avatar

Might anyone have some ideas? I am searching for 2 days now without any step forward....

pmall's avatar

Show you controller action please

Please or to participate in this conversation.