JSON response from PHP for ajax
-
I'm trying to create a php file that always returns a json as response. The problem is, it looks like it's returning a valid JSON, but jquery.ajax always says "invalid json." I copied and pasted the response and stuck it in jsonlint.com and it says it's valid json. Can anyone point out what I'm doing wrong here? Why can't jquery (and other json parser) parse this even it's a valid json?
header('Content-type: application/json');
include("database.php");
require_once "../lib/JSON.php";$json = new Services_JSON();
$method = strtolower($_SERVER['REQUEST_METHOD']);
$value = array();
// Open the database connection first, which will be used later.
$DB = new DBConnect();
$DB->NewConnection("localhost", "db_name", "db_role", "db_pw" );
// assume method is get here
if ($method == "get"){
$value = getEmployees();
}else if ($method == "post"){
}else if ($method == "put"){
}else {
echo "Error - unknown http request method";
}// tried without callback stuff but didn't work either
echo $_GET['callback'] . '(' . $json->encode($value) . ');'; -
I'm trying to create a php file that always returns a json as response. The problem is, it looks like it's returning a valid JSON, but jquery.ajax always says "invalid json." I copied and pasted the response and stuck it in jsonlint.com and it says it's valid json. Can anyone point out what I'm doing wrong here? Why can't jquery (and other json parser) parse this even it's a valid json?
header('Content-type: application/json');
include("database.php");
require_once "../lib/JSON.php";$json = new Services_JSON();
$method = strtolower($_SERVER['REQUEST_METHOD']);
$value = array();
// Open the database connection first, which will be used later.
$DB = new DBConnect();
$DB->NewConnection("localhost", "db_name", "db_role", "db_pw" );
// assume method is get here
if ($method == "get"){
$value = getEmployees();
}else if ($method == "post"){
}else if ($method == "put"){
}else {
echo "Error - unknown http request method";
}// tried without callback stuff but didn't work either
echo $_GET['callback'] . '(' . $json->encode($value) . ');';It seems that everything is fine. Probably, the code segment is going on the last else statement, and so, it prints an invalid Json line there. Try debugging that particular location, and you probably would be able to find it.
-
I'm trying to create a php file that always returns a json as response. The problem is, it looks like it's returning a valid JSON, but jquery.ajax always says "invalid json." I copied and pasted the response and stuck it in jsonlint.com and it says it's valid json. Can anyone point out what I'm doing wrong here? Why can't jquery (and other json parser) parse this even it's a valid json?
header('Content-type: application/json');
include("database.php");
require_once "../lib/JSON.php";$json = new Services_JSON();
$method = strtolower($_SERVER['REQUEST_METHOD']);
$value = array();
// Open the database connection first, which will be used later.
$DB = new DBConnect();
$DB->NewConnection("localhost", "db_name", "db_role", "db_pw" );
// assume method is get here
if ($method == "get"){
$value = getEmployees();
}else if ($method == "post"){
}else if ($method == "put"){
}else {
echo "Error - unknown http request method";
}// tried without callback stuff but didn't work either
echo $_GET['callback'] . '(' . $json->encode($value) . ');';what is the response? what does firebug say?