• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

AJAX Pulling JSON information from PHP

Newbie Spellweaver
Joined
May 23, 2014
Messages
63
Reaction score
10
Basically, the idea is I want the AJAX to run the lovedj script (below) and then return the encoded JSON information, to put into a notification box. However I seem to be getting the error 'Uncaught TypeError: Cannot read property 'msg' of null'

jQuery Script:
Code:
_buildLove: function() {        $.ajax({
            type: 'POST',
            url: "web-structure/inc/radio/lovedj.php",
                    type: "POST",
            dateType: "json",
            success: function(data) {
                alert(data.msg); 
            }             
        });
    },


LoveDJ script:
PHP:
<?php    
        require_once '../../../staff/_inc/glob.php';
        $query  = $db->query( "SELECT * FROM connection_info ORDER BY id DESC LIMIT 1" );
        $array  = $db->assoc( $query );
        $stats = $core->radioInfo( "http://{$array['host']}:{$array['port']}" );
        
        $usercount = $db->query("SELECT * FROM users WHERE username = '".$stats['streamtitle']."'");
        $growl = array();
        
        if ($db->num($usercount) == 0) {
        
            $growl['title'] = "This user is not in our database";
            $growl['msg'] = "Sadly, this user is not in our database, we will look into this asap.";
            $growl['error'] = true;
            
        }
        else {    
            $query2 = $db->query("UPDATE users SET likes = likes + 1 WHERE username = '".$stats['streamtitle']."'");
            $growl['title'] = "";
            $growl['msg'] = "";
            $growl['error'] = false;
        }
    
        
        header("Content-Type: application/json", true);
        json_encode($growl); 
?>

JSON (var_dump of the script):
Code:
[COLOR=#00008B]string[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]135[/COLOR][COLOR=#000000])[/COLOR][COLOR=#800000]"{"[/COLOR][COLOR=#000000]title[/COLOR][COLOR=#800000]":"[/COLOR][COLOR=#2B91AF]This[/COLOR][COLOR=#000000] user [/COLOR][COLOR=#00008B]is [/COLOR][COLOR=#00008B]not [/COLOR][COLOR=#00008B]in [/COLOR][COLOR=#00008B]our[/COLOR][COLOR=#000000] database[/COLOR][COLOR=#800000]","[/COLOR][COLOR=#000000]msg[/COLOR][COLOR=#800000]":"[/COLOR][COLOR=#2B91AF]Sadly[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]this[/COLOR][COLOR=#000000] user [/COLOR][COLOR=#00008B]is [/COLOR][COLOR=#00008B]not [/COLOR][COLOR=#00008B]in [/COLOR][COLOR=#00008B]our[/COLOR][COLOR=#000000] database[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] we will look [/COLOR][COLOR=#00008B]in to [/COLOR][COLOR=#00008B]this[/COLOR][COLOR=#000000] asap[/COLOR][COLOR=#000000].[/COLOR][COLOR=#800000]","[/COLOR][COLOR=#000000]error[/COLOR][COLOR=#800000]":true}"[/COLOR]
 
◝(⁰▿⁰)◜Smile◝ (⁰▿⁰)◜
Developer
Joined
May 29, 2007
Messages
2,167
Reaction score
899
json_encode returns a string so normally you've to echo out the string.

Try this:
PHP:
echo json_encode($growl);
 
Newbie Spellweaver
Joined
May 23, 2014
Messages
63
Reaction score
10
json_encode returns a string so normally you've to echo out the string.

Try this:
PHP:
echo json_encode($growl);

Doing this echos out
Code:
[COLOR=#000000]{"title":"This user is not in our database","msg":"Sadly, this user is not in our database, we will look into this asap.","error":true}[/COLOR]


EDIT: My apologies CodeDragon, I didn't understand what you meant by echoing it out. It actually worked! I appreciate the help, you don't understand how long I've been stressing over this for!

If the error's coming from PHP, you aren't dumping your $growl object at the right time. The error is saying that $growl, i.e. the object/array that's supposed to contain the 'msg' property, is null.

That error is coming from the javascript.

That var_dump isn't part of the actual script, I just dumped it to show you that it was infact returning JSON and according to the outputted JSON the msg parameter isn't null though.
 
Last edited:
◝(⁰▿⁰)◜Smile◝ (⁰▿⁰)◜
Developer
Joined
May 29, 2007
Messages
2,167
Reaction score
899
Doing this echos out
Code:
[COLOR=#000000]{"title":"This user is not in our database","msg":"Sadly, this user is not in our database, we will look into this asap.","error":true}[/COLOR]
That error is coming from the javascript.

Make sure you echo it out. On the javascript side you receive a full string, you've to decode the json first before you can use it (this is how I do it and it works for me), I know that there is a data type but I never really used it. Below is some example code I used.

Code:
        var req = $.ajax({
            type: "GET",
            url: 'LINK-HERE'
        }).done(function (msg) {
            var data = JSON.parse(msg);
        }).fail(function ( ) {
            alert('Failed.');
        });
 
Newbie Spellweaver
Joined
May 23, 2014
Messages
63
Reaction score
10
Make sure you echo it out. On the javascript side you receive a full string, you've to decode the json first before you can use it (this is how I do it and it works for me), I know that there is a data type but I never really used it. Below is some example code I used.

Code:
        var req = $.ajax({
            type: "GET",
            url: 'LINK-HERE'
        }).done(function (msg) {
            var data = JSON.parse(msg);
        }).fail(function ( ) {
            alert('Failed.');
        });

In that code it only shows parsing the msg if I was to parse multiple arrays like 'title' and error would I just copy that line and change the msg to title or error or whatever?
 
◝(⁰▿⁰)◜Smile◝ (⁰▿⁰)◜
Developer
Joined
May 29, 2007
Messages
2,167
Reaction score
899
In that code it only shows parsing the msg if I was to parse multiple arrays like 'title' and error would I just copy that line and change the msg to title or error or whatever?

It just takes in the full string of the page (we assume it's a json string), then it decodes the string to a array inside JavaScript.
 
Back
Top