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.

4
  • You have to echo results to AJAX. Commented Apr 23, 2015 at 18:14
  • What happens when you return $data from getAvailableResponders() ? Commented Apr 23, 2015 at 18:17
  • Then I get the input data needed to perform the query. Commented Apr 23, 2015 at 18:20
  • Most likely related to: stackoverflow.com/questions/5351236/… Commented Apr 23, 2015 at 18:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
Sorry.. Yes.. I am using Laravel
I tried that approach but no feedback at all. See the screenshots added
Could it be that the callback is already executed before the database has been queried?
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.