I personally think PHP and JavaScript have their own strengths. If they can be perfectly combined, it will create a lot of miracles! I wrote this class for the first time. Article Bug is inevitable. please correct me ~
Currently, Ajax applications are very popular. In Ajax, communication between client JavaScript and Server Dynamic scripts is critical. If the transmitted information is relatively simple, we usually use the string method directly. If the structure of the information is more complex, XML documents are commonly used. Although XML documents are widely used, it is not easy to use PHP to generate and process them with JavaScript. Here I recommend a simplified alternative to XML documents: JSON!
First, let's take a look at the basic knowledge of JavaScript.
1. Create an array. You can use the built-in class array in JS to initialize the array, or use the JSON symbol "[]". The arr1 and arr2 created in the following two ways are essentially the same:
VaR arr1 = new array ();
Arr1 [0] = "apple ";
Arr1 [1] = "google ";
Arr1 [2] = "longbill ";
VaR arr2 = ["apple", "Google", "longbill"];
Note that the index of the array can also be a string, for example, arr1 ["name"] = "longbill"; then the array is equivalent to an object...
2. Create an object. You can use the built-in Class Object in JS to initialize the object, or use the JSON symbol "{}". The essence of the obj1 and ob2 created in the following two ways is the same:
VaR obj1 = new object ();
Obj1.name = "longbill ";
Obj1.age = 18;
VaR obj2 = {name: "longbill", age: 18 };
Note: Here, "{" and "}" must be written in the form of "key: value", and different "key: Value" must be separated. The "key" can also contain special characters such as spaces, which must be referenced by "" (quotation marks), such as "phone number": 123456
In JS, an array is essentially an object, and an object itself is an array. Therefore, obj1.name and obj1 ["name"] are the same references.
We can also use the nesting of JSON symbols to define complex s objects:
VaR people = [
{
Name: "longbill ",
Age: 18
},
{
Name: "Neal ",
Age: 19
},
{
Name: "glocklee ",
Age: 17
}
];
// Understand this ~~
Most XML documents can be expressed in JSON:
<? XML version = "1.0"?>
<Root>
<Book>
<Name> Ju love </Name>
<Price> $15 </price>
</Book>
<Book>
<Name> expose Crip </Name>
<Price> $25 </price>
</Book>
</Root>
If JSON is used, it can be expressed:
{
Root:
[
{
Name: "Ju Love ",
Price: "$15"
},
{
Name: "JavaScript ",
Price: "$25"
}
]}
Is it much simpler?
In addition, the processing of client JS is also very simple. As long as the string function "eval" is executed, JSON information can be extracted. If it is an XML document, there is no need to use a large number of standard DOM operations to extract the data. For example:
// A json file (string) has been downloaded from the server using Ajax and saved in the variable JSON.
Eval ("Var myvar =" + JSON );
// In this way, the information in JSON is expressed in the variable myvar.
Disadvantage: If the JSON format is incorrect, the server JS system may be incorrect or even crashed.
Solution:
Try before Eval, as shown in figure
// A json file (string) has been downloaded from the server using Ajax and saved in the variable JSON.
Try {
Eval ("Var myvar =" + JSON );
} Catch (e) {alert ('json syntax error! ');}
// In this way, even if the JSON format is incorrect, only one prompt box will pop up without throwing a script error!