1

I am making an AJAX call to my PHP script, via $.getJSON. The problem is, PHP is returning json-encoded data, but my script is not console.logging anything at all.

function Club(){
    this.URL = "http://localhost/imp03/includes/ajaxCall.php";
}
Club.prototype.loadData = function(){
    $.getJSON(Club.URL, function(data){
        console.log(data);
    });
}

There are no errors in the console, and this is my PHP script.

$db = new Database(HOST, USER, PASS, DB);
$array = $db->getData();
header('Content-Type: application/json');
echo json_encode($array);
exit;

This is the method in my Database class, responsible for getting data

public function getData(){
    try{
        $sql = $this->db->prepare("SELECT * FROM inlever3");
        $sql->execute();
        $result = $sql->fetchAll();
        return $result;
    } catch(PDOException $e){
        echo $e;
        return false;
    }
}
6
  • first of all, $.getJSON need server to return json type data, json_encode(array) is needed Commented Apr 25, 2014 at 9:49
  • if possible, add a jsfiddle you will get more responses. Commented Apr 25, 2014 at 9:50
  • What do you see when you navigate to http://localhost/imp03/includes/ajaxCall.php in your browser ? Commented Apr 25, 2014 at 9:50
  • @TechStone I have edited my code, included the header and I am json_encoding my data, still no change. Commented Apr 25, 2014 at 9:52
  • @Dave: It is hard to include a jsFiddle, as I can not include PHP. Holt: I am seeing the JSON data I am asking for. ;) Commented Apr 25, 2014 at 9:53

2 Answers 2

3

Club is the function, which doesn't have the URL property.

Club.prototype.loadData = function(){
    $.getJSON(Club.URL, function(data){
        console.log(data);
    });
}

should be

Club.prototype.loadData = function(){
    // here change Club.URL to this.URL
    $.getJSON(this.URL, function(data){
        console.log(data);
    });
}

And use it like:

var club = new Club();
club.loadData();
Sign up to request clarification or add additional context in comments.

Comments

2

Just tried it, it works as intended:

You need to use this.URL not Club.URL on the $.getJSON() call

test.php

<?php

if (isset($_GET['loadData']))
{
    // dummy data
    exit(json_encode(
        array('hello' => 'world')
    ));
}

?>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">

function Club(){
    this.URL = "test.php?loadData";
}
Club.prototype.loadData = function(){
    $.getJSON(this.URL, function(data){
        console.log(data);
    });
}

var c = new Club();
c.loadData();

</script>

4 Comments

It wont make any change !!
@Vimalraj.S what do you mean?
there might be problem in his URL
@LatheesanKanes OOps sorry , here he could even use c.URL

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.