I have always been trying to avoid using most of the HTTP protocol's properties for the sake of fear of the unknown.
However, I said to myself that I'm going to face fear today and start using headers purposefully. I have been trying to send json
data to the browser and use it right away. For example, if I have an Ajax handler function on ready state 4 which looks like so:
function ajaxHandler(response){
alert(response.text);
}
And I have set the content-type header in my PHP code:
header('Content-Type: application/json');
echo json_encode(array('text' => 'omrele'));
Why can't I directly access the property from the handler function, when the browser is clearly told that the incoming data is application/json
?
text
as a javascript variable in the handler and not response? That would be very weird functionality. The json_encode also creates 1 object out of your PHP array. So when you get this into javascript it needs to be assigned to a variable. – FlashinJSON.parse()
. You could take some different action, or force an error if the wrong contentType appears. – user1864610response.text
is still a string. – nnnnnn