0

I would like to retrieve some database data where the query is executed initially by an AJAX call.

JAVASCRIPT

$('form[incidentForm]').on('submit', requestResponders  );

var requestResponders = function(e) {
    e.preventDefault();
    getResponders(latitude, longitude, function(data) {
        console.log(data);
    });
}

function getResponders(latitude, longitude, callback) {

    var geoInformation = [];
    geoInformation.push({name:'latitude', value:latitude});
    geoInformation.push({name:'longitude', value:longitude});

    $.ajax({
        type: "POST",
        url: '../searchResponders',
        data: geoInformation,
        success: function(responders) {
            callback(responders);
        }
    })
};

ROUTES

Route::post('searchResponders','RespondersController@getAvailableResponders');

PHP

public function getAvailableResponders(Request $request) {
    $data = $request->all();
    $responder = ResponderManagement::searchResponders($data);
    return $responder;
}

public function searchResponders($geoInformation) {
    $lat = $geoInformation['latitude'];
    $lng = $geoInformation['longitude'];
    $results = DB::select(DB::raw("SELECT STATEMENT));
    return $results;
}

When I use POSTMAN to check the functionality, everything works fine. I get the wished for response and a json is returned with the database entries searched for.

enter image description here

When I use the AJAX call, I receive an empty array. enter image description here enter image description here

Therefore I guess, that due to the async behaviour, the return value is already returned, before the query in the DB has finished.

But I don't see why. I the ajax call, only on success I call the callback method to show the data. So I would guess the database query should have finished by that time already.

Can someone support me here please?

UPDATE 1: I updated my function now

public function searchResponders($geoInformation) {
    $lat = $geoInformation['latitude'];
    $lng = $geoInformation['longitude'];
    $results = DB::select(DB::raw("SELECT STATEMENT));
    echo json_encode($results);
}

But the returned value when logged in the console is still empty. whereas in the POSTMAN POST call everything is returned

Update 2 I grabbed a screenshot showing the content of the AJAX call in a chrome debugger. But this seems fine to me? Can someone help me out here? The response is still empty, but the direct call works fine.

enter image description here

UPDATE 3 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.

sesc360
  • 3,155
  • 10
  • 44
  • 86

1 Answers1

1

return does not send results to AJAX, you have to echo the output. Wrap the output in json_encode() is it is an array, or any other complex structure other then a string or number. I don't think it hurts to just always use json_encode.

Dan
  • 10,614
  • 5
  • 24
  • 35
  • Depends on the framework that he's using, which we don't really know anything about. I am guessing it's Laravel from the `Routing::post` call, but it's not definitive. Also he has already stated that it works if you call it directly, but not when it is called via an AJAX call. – Populus Apr 23 '15 at 18:19
  • Sorry.. Yes.. I am using Laravel – sesc360 Apr 23 '15 at 18:20
  • I tried that approach but no feedback at all. See the screenshots added – sesc360 Apr 23 '15 at 18:30
  • Could it be that the callback is already executed before the database has been queried? – sesc360 Apr 23 '15 at 18:37
  • Its possible, I'm no JS expert, but if you put `console.log(responders);` as the first line in your success function, you will know if the server is giving you any data. – Dan Apr 23 '15 at 18:48
  • I don't have a clue whats going on here... I see the output cleary in the API call – sesc360 Apr 23 '15 at 18:49
  • Interesting.. I just did this.. and the result is empty – sesc360 Apr 23 '15 at 18:50
  • Might be helpful to add and error function to the ajax call `error: function(e){ console.log(e); }` – Dan Apr 23 '15 at 18:54