HTTP Content-Type Header and JSON
- 论坛
- HTTP Content-Type Header and JSON
22 浏览
HTTP Content-Type Header and JSON
为了对未知的恐惧,我一直试图避免使用HTTP协议的大多数属性。
然而,我告诉自己今天我要面对恐惧,开始有目的地使用头部信息。我一直试图将json
数据发送到浏览器并立即使用它。例如,如果我有一个Ajax处理函数在准备就绪状态4时,代码如下:
function ajaxHandler(response){ alert(response.text); }
我在我的PHP代码中设置了content-type头部:
header('Content-Type: application/json'); echo json_encode(array('text' => 'omrele'));
为什么当浏览器明确告知传入数据是application/json
时,我不能直接从处理函数中访问该属性?