0

I am getting SyntaxError:JSON.parse in the ajax error function and when trying to validate via json validator, it says that the header was undefined and was expecting {,[ Is my header code incorrect? Many thanks

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
$json = "";
$json .= "{\n";
$json .=  "\"company\": \"".$company."\",\n";
$json .=  "\"box\": \"".$box."\",\n";
$json .=  "\"dept\": \"".$dept."\",\n";
$json .=  "\"submit\": \"".$submit."\",\n";
$json .=  "\"service\": \"".$service."\",\n";
$json .=  "\"address\": \"".$address."\",\n";
$json .=  "\"authorised\": \"".$authorised."\"\n";
$json .= "}\n";
echo $json;
5
  • 1
    Show us the actual JSON output, please. Commented Jul 25, 2011 at 10:37
  • @matt there is no output. firebug is only showing html in response. Thanks Commented Jul 25, 2011 at 10:50
  • @bollo - that sounds like you are fetching the wrong URL Commented Jul 25, 2011 at 11:00
  • And has nobody heard of interpolation? Or heredoc? Commented Jul 25, 2011 at 11:01
  • @quentin url is correct. After the suggestion by Dhruv, the error has gone, but I am still not seeing JSON in firebug to show the data is being returned. Commented Jul 25, 2011 at 11:04

1 Answer 1

3

Do not try to write your own json. JSON easily becomes invalid if it has unescaped quotes,invalid characters etc.

Better store everything in php array and then use PHPs inbuilt

json_encode to create the json .

see example here : http://codepad.org/Iaa0zx9J

<?php

$dataArr = array(
"company" => "abc corp",
"dept" => "finance",
"submit" => "100"
);

$myJsonString = json_encode($dataArr);
echo $myJsonString ;

?>
Sign up to request clarification or add additional context in comments.

1 Comment

how would that work for variables? for example, in my code I would need to include variables in the return. Would they just be used like thus: "company" => ".$company.", thanks

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.