0

I have using ajax. Here creating json in PHP

$(document).ready(function(e) {
        var data='page=1';
        $('#loader_tracker').show();
        $.ajax({
        type: "GET",
        url: "getDetails.php",
        data:data,
        dataType:"json",
        success:function(data) {
            $('#loader_tracker').hide();
        },
         error:function(jqXHR, textStatus, errorThrown){
         console.log(errorThrown);
         console.log(textStatus);
         },
         complete: function(){
            $('#loader').hide();
        }

        });
        return false;

});

PHP COde

require_once('dbConnection.php');
$connection = new dbconnection();
$con = $connection->connectToDatabase();
header('Content-type: text/json'); 
die($response);

$vendorId = 2;
$name = "sample";
$latitude = 100;
$longitude = 200;

$device_details=array('devices' => array());

    for($i=1;$i<=20;$i++)
{

$details =array('id' => $vendorId, 'name' => $name, 'lat' => $latitude, 'lan' => $longitude);
array_push($device_details['devices'],$details);
}

echo json_encode($device_details);

It gives SyntaxError {stack: (...), message: "Unexpected token <"} Error

I cant find issue for this. I dont know how to add json format like this one[{devices:[{...}]}]

1
  • getDetails.php in this file check that there is no HTML on the top Commented Mar 19, 2014 at 6:46

3 Answers 3

1

Remove the line below (which makes your response be invalid json):

print_r ("$device_details");
Sign up to request clarification or add additional context in comments.

2 Comments

I have add this line for testing.. Even i have remove this line, same error occur
I dont know where it is. i think its json format error
0

Remove to debug code line print_r ("$device_details"); from PHP file, And use echo json response and terminate code.

header('Content-type: text/json'); 
die($response);

And also add header sometime create problem with content information so please add proper header information.

1 Comment

@Mohaideen, can you add ajax response in question??
0

Change the following code

print_r ("$device_details");
$response=json_encode($device_details);
print $response;

to,

echo json_encode($device_details);
die();

5 Comments

Now i get this error SyntaxError {stack: (...), message: "Unexpected token ["}
can you replace array_push($device_details['devices'],$details); to $device_details['devices'][] = $details;
can you show the response string ? Or the result when you directly visit that page getDetails ? I thinks its json formating error
I have modify my question. see above
try $device_details=array(); for($i=1;$i<=20;$i++) { ... array_push($device_details,$details); }

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.