0

Please can anyone assist I'm trying to get my JSON data displayed on my html5 localhost page,

I'm still new to JSON

I get the following returned but no data is loading on the page.

http://www.hostname/getCheck.php?callback?&callback=jQuery110205560797746881064_1392215061343&_=1392215061344

Please if anyone can assist.

Below is my php script

`mysql_select_db($database_xxx, $xxx); $rsfet = "SELECT * FROM cs_tracking "; $fet = mysql_query($rsfet, $xxx) or die(mysql_error()); $json = array(); while($r=mysql_fetch_array($fet)){ $json[] = $r; }

header('Access-Control-Allow-Origin: *');
echo $callback ='('.json_encode($json).')';`

and my javascript to display the table data

` 
        $(document).ready(function(){
                $.ajax({
    url: 'http://xxxxxxxxxxx.com/getCheck.php?callback=?',
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    jsonp: true,
    success: function(data){
               $.each(data,function(i,photo){
                 var tblRow =""
    +""+data.CS_Track_Child+""
    +""+data.CS_Track_Date+""
    +""+data.Tracking_Status+""
    +""+data.CS_Tracking_ID+""
    +"" ;
    $(tblRow).appendTo("#userdata tbody");
     });
     },    

     });
    });`

2 Answers 2

0

The $callback variable is not magically declared in your script (at least, it shouldn't be); you can access the value via $_GET['callback'] but make sure to sanitize its value:

if (isset($_GET['callback']) && preg_match('/[A-Z]\w*/i', $_GET['callback']) {
    header('Content-Type: application/javascript');
    header('Access-Control-Allow-Origin: *');
    printf('%s(%s);', $_GET['callback'], json_encode($json));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried the code and my php script is generating the JSON, my table displays undefined for each value.
0

You have two GET parameter of callback one is valid but empty and second is invalid.

http://www.hostname/getCheck.php?callback?&callback=jQuery110205560797746881064_1392215061343&_=1392215061344

url: 'http://xxxxxxxxxxx.com/getCheck.php?callback=?',

So remove your parameter and try with this:

url: 'http://xxxxxxxxxxx.com/getCheck.php',

2 Comments

i have found the problem;
I didn't call the correct data inside the table. i used data.CS_Track_Child instead of item.CS_Track_Child, i also changed (i,photo) to (i,item) Thank You for all the help.

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.