我想给出一个简单的true响应,但根据各种 JSON 解析器,这不是有效的JSON:
true
Run Code Online (Sandbox Code Playgroud)
但是,true在编码和解码时,PHP和Javascript的行为类似于"true"确实是有效的JSON :
PHP的
echo json_encode( true ); // outputs: true
echo json_decode( true ); // outputs: 1
echo gettype(json_decode( true )); // outputs: boolean
Run Code Online (Sandbox Code Playgroud)
jQuery-
JSON.stringify( true ); // outputs: true
jQuery.parseJSON( true ); // outputs: true
typeof jQuery.parseJSON( true ); // outputs: boolean
Run Code Online (Sandbox Code Playgroud)
那么发送true格式为JSON 的响应的正确方法是什么?验证人都错了吗?