Response of ajax request is different on my local environment and my live server

1.1k Views Asked by At

In my local environment my ajax response is a json object as expected which only contains the objects and their value pairs I requested from an sql database, but once uploaded to hostgator the response is much larger and my object can only be found in the responseText value of the response object.

The responseText property doesn't exist in my local environment so accessing it directly in my live environment would put my local and live code out of sync and likely cause problems when doing updates plus responseText is a string containing my objects preceded by // making it difficult to access without parsing and type casting.

I was going to post the objects but they don't format well unless I recreate them and the response from the hostgator sever is too large to recreate. Instead here is how my php script is returning the sql data.

    $db = new PDO('mysql:host='.constant('DB_HOST').';dbname='.constant('DB_NAME').';charset=utf8', constant('DB_USER'), constant('DB_PW'));

    $sth = $db->prepare('SELECT `index`, Todays_Date, First_Name, Middle_Name, Last_Name, Telephone, Position  FROM application');
    $sth->execute();

    $result = $sth->fetchAll(PDO::FETCH_ASSOC);

    $row_count = count($result);

    if($row_count > 0){
        echo json_encode($result);
    }else{
        echo "No records found.";
    }

    // ajax call update 
    var listAjax = function() { 
    $.ajax({
        url: "api/listAll.php",
        dataType: "json",
        type: "GET",
        data: {},
    }).done(function(data) {
        buildList(data);
        console.log(data);
    }).fail(function(data) {
        //console.log(data);
    })
}

Maybe there is a better way to return my json object from php? or maybe this is a common problem I haven't ran into. Let me know if need to see anything else from the response object. Thanks for any help.

Update: Added ajax code. Did see that the longer response from server with response text did not come through with the commented out fail, but it does have a status 200. Tried to post images of it but stack overflow won't let me. Here is a link to a photo of the response output: http://i1378.photobucket.com/albums/ah114/Dakota_Hipp/IMG_1158_zps32xytuqx.png

1

There are 1 best solutions below

0
On BEST ANSWER

Per Discussions:

In the image shown, there is an // being added to the beginning of your echoed data which is making your JSON invalid. You need to backtrace through your included files inside your requested URL: url: "api/listAll.php" and remove the // to make your JSON valid again.