0

i have the following code: Javascript object:

 var getDBresults = (function () {  
    function getResult(url,TableName ,callback){
        $.ajax({
            url: url,
            type: 'POST',
            data: {
                'table':TableName,
            },
            dataType: 'json',
            success: function(data){
                callback(data);
                console.log(data)

            },
            error: function(){}
        });
    }   
    return {
        getAllVideoes: function(){
            getResult("getAllResults.php", "videoer", function(data){
                return data;
            });
        }
    }

})();

simple php script:

<?php

    $tableName = $_REQUEST['table'];

    echo $tableName;
?>

My js command for fetching(seperate script ofc):

    var obj = getDBresults;
    var data = obj.getAllVideoes();
console.log(data)

My issue is with the callback function. It wont output anything and it doesn't seem to be running at all.. Had this issue for quite som time now and i just cant figure it out.. Is there anything i'v been missing? All help is apriciated! Sorry for my spelling btw.

Rinrub
  • 137
  • 10
  • On the JS side, have you tried using a debugger (firebug, et al.) to make sure the callback is being triggered? Also, I'd add a logging statement to your error callback. I'm not sure about your PHP, it doesn't look right to me, but I'll let someone who knows more about PHP comment on that :) – James Adam Mar 10 '14 at 13:11
  • _“and it doesn't seem to be running at all”_ – don’t _assume_ what “seems” to be happening or, not – __verify__ it. Your browser has a bunch of developer tools build in to debug stuff like this, so familiarize yourself with how to use them. – CBroe Mar 10 '14 at 13:17
  • yeah, im using firebug and the callback is not running. thats why i thought it might be a syntax problem. thx for the reply – Rinrub Mar 10 '14 at 13:21

1 Answers1

0

you ajax is expecting

`dataType: 'json',`

json output from your php script

you are returning html/plaintext data

`echo $tableName;`

try json_encode

echo json_encode(array($tableName)); 

result is not in JSON format, so when jQuery fails to parse it,

You can catch the error with ajax's error: callback function

Ravikumar Sharma
  • 3,678
  • 5
  • 36
  • 59