When json is used to transmit data in ajax, other data types are not a problem. However, if the JSON generated by the server contains bool data, a small problem occurs during client parsing, summary:
The JSON returned by the server is:
Copy codeThe Code is as follows:
{"TypeID": [1037], "Title": "HEBEI software Vocational and Technical College", "Intro": "", "IsLink": "false", "LinkUrl ": "http://www.hbsi.edu.cn", "IsPic": "true", "Picture": "/newsimages/hbsi.jpg", "Content": "<p> <br> </p> "}
Here, attributes: IsLink and IsPic are both of the bool type. How to use them on the client:
Copy codeThe Code is as follows:
Document. getElementById ("checkbox1"). checked = news. IsLink;
Check box is selected, but IsLInk is false. This check box should not be selected. Why?
Check the cause. javascript has three basic data types (string, number, and boolean) and two reference data types (Object and Array) and two special data types (Null and Undefined ). The following principles apply when converting other types to the bool type:
Value after the data type is converted to bool
Null FALSE
Undefined FALSE
Object TRUE
Function TRUE
0 FALSE
1 TRUE
A number other than 0 and 1 is TRUE.
String TRUE
"" (Null String) FALSE
In this case, IsLink is the string "false" in JSON, so the bool type is true after conversion.
Solution:
Copy codeThe Code is as follows:
Document. getElementById ("checkbox1"). checked = news. IsLink = "true ";