1

I have the weird Problem that I get no response from my Ajax Call. I get the data but when im trying to return it I only get an blank screen. There are 3 Files in my project which are needed because my project has so many DB calls and functions so its easier to read.

So this one of many functions which calls my ajaxCall function

$.when(ajaxCall("getOutlets", '', null)).done(function(res) {
    if(res) {
        console.log(res);

this is my global ajaxCall Function for the whole project:

function ajaxCall(functionName, params, answers) {
    return jQuery.ajax({
        type: "POST",
        url: '/ajax/ajax.php',
        dataType: 'json',
        data: {
            functionIdentifier: functionName, 
            functionParams: params,
        },

my Ajax.php file then calls based on the functionName Param the function in my database.php file. Ajax.php:

$response = array();

switch ($functionIdentifier) {
    case 'getOutlets':
        $response = $database->getSearchOutletsFromDatabase($functionParams);
        break;
    .... many switch cases
}

echo json_encode($response);

die();
?>

and finally in my database.php It calls the mysqli functions which looks like this:

public function getSearchOutletsFromDatabase() {
    $result = mysqli_query($this->conn, "CALL `sp_outlets_get`()") or die(mysqli_error($this->conn));   
        while($row = $result->fetch_assoc()) {
            $response[] = $row;
        }

        @mysqli_next_result($this->conn);
            return $response;
        }

and here is the weird part. If im returning only one Object from $result like return $result->fetch_object() I return the first Object successfull. BUT when I just save the row in an array and want to return my array the whole response is empty. No errors. Nothing.

If you need more information just say it Ill try to add more.

Edit: If Im putting this on top of my ajax.php file where the response gets returned I get following error on Developers Network Tab:

header('Content-Type: application/json');
 $response = array();

    switch ($functionIdentifier) {
        case 'getOutlets':
            $response = $database->getSearchOutletsFromDatabase($functionParams);
            break;
        .... many switch cases

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

15
  • Have you looked at the "Network" developer tab to see what's actually being sent and returned? Commented Aug 20, 2018 at 14:33
  • why did you put the die() at the end of the php script? Commented Aug 20, 2018 at 14:35
  • I get a Status 200 and all looks fine but in Network Tab nothing gets returned. Thats my problem. No Errors. Nothing. Just a white blank screen. But in my Database function if I fetch an object and print it, it has data. Just when I save it in the array and return the array it doesnt work anymore... Commented Aug 20, 2018 at 14:35
  • @sietse85 yeah its on the end Commented Aug 20, 2018 at 14:36
  • but why did you put it there Commented Aug 20, 2018 at 14:36

1 Answer 1

1

I found what caused the error (and sometimes it is one of the easiest things). Well it was ö,ä,ü etc in my response. Adding a utf8_encode before I return my data to ajax solved the problem.

public function getSearchOutletsFromDatabase() {
    $result = mysqli_query($this->conn, "CALL `sp_data_get`()") or die(mysqli_error($this->conn));  
    $response = array();
    while($row = $result->fetch_assoc()) {
        $res['ID'] = $row['ID'];
        $res['Name'] = utf8_encode($row['Name']);
        array_push($response, $res);
    }
    @mysqli_next_result($this->conn);
    return $response;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.