I can't find any solutions to this problem.
I'm having hard time with this code. Based on what I searched:
HTTP/1.1 200 OK
- means that the page is good or OK.
I don't understand this header function parts only.
It is actually part of code.
My questions are:
Why this code is sending
header('HTTP/1.1 200 OK');
? I know this code means that the page is good, but why are we sending this code?What is cache-control part, and what will happen if the code send that?
What is
Expires:
, and the date is 1970? (please simple explanation)What will happen if the code send header
('Content-type: application/json');
this part, and why are we sending this ?
Code is here:
function json_response( $data, $error=false ) {
if( $error )
header('HTTP/1.1 500 JSON Error');
else
header('HTTP/1.1 200 OK');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
header('Content-type: application/json');
// Convert strings/integers into an array before outputting data...
if(!is_array($data))
echo json_encode(array($data), true);
else
echo json_encode($data, true);
exit;
}
This tells your browser that the requested script was found. The broswer can then assume it will be getting some other data as well.
This tell the browser, and intermediate caches, DO NOT CACHE the data I am sending. This is so that when you make a second request for this data, it will have to go to your server and rerun the data gathering process rather tha get the data from the browser cache or an intermediary cache somewhere on the internet, between your broswer and your server.
This is again for the cache control. Is says that the cache should expire in 1970, in other words if you have it cached you should remove it because 1970 was a long time ago.
This is to tell the browser that the data you are sending is in JSON format and therefore how to treat it, in your case it means convert the JSON String that is being sent to a javascript Object so the javascript code can process it as a native object and not have to convert the JSON String to a Javascript object manually.