When it comes to AJAX, we will inevitably face two questions.
The first is what format does AJAX use to exchange data?
The second is how to solve cross-domain requirements?
There are currently different solutions to these two problems. For example, data can be described with custom strings or XML, and cross-domain issues can be solved through server-side proxies.
But so far, the most recommended or preferred solution is to use JSON to transmit data and rely on JSONP to cross domains. And that’s what this article is about.
Although there is only one letter difference between JSON and JSONP, they are actually not the same thing at all: JSON is a data exchange format, while JSONP is an unofficial cross-domain format created by the ingenuity of developers. Data exchange protocol. Let's use the recent popular spy movie as an analogy. JSON is the "code" used by underground parties to write and exchange information, while JSONP is the connection method used to transmit information written in code to their comrades. did you see? One is to describe the format of the information, and the other is the method agreed upon by both parties for transmitting the information.
What is JSON?
As I briefly mentioned before, JSON is a text-based data exchange method, or a data description format. Whether you should choose it or not, you must first pay attention to its advantages.
Advantages of JSON:
Based on plain text, cross-platform transmission is extremely simple;
Javascript native support, almost all backend languages are supported;
Lightweight data format, occupying very few characters, especially suitable for Internet transmission;
Highly readable, although not as clear as XML, it is still easy to identify after reasonable indentation;
Easy to write and parse, of course, provided you know the data structure;
Disadvantages of JSON Of course there is, but in the author's opinion it is really insignificant, so I won't explain it separately.
The format or rules of JSON:
JSON can describe the data structure in a very simple way. It can do everything XML can do, so the two are completely equal in terms of cross-platform.
JSON has only two data type descriptors, curly brackets {} and square brackets []. The remaining English colons are mapping characters, English commas are delimiters, and English double quotes "" are defining characters.
Braces {} are used to describe a set of "unordered key-value pairs of different types" (each key-value pair can be understood as an attribute description of OOP), and square brackets [] are used to describe a set of "unordered key-value pairs of different types". "Ordered data collection" (an array that can correspond to OOP).
If there are multiple sub-items in the above two sets, they should be separated by commas.
Key-value pairs are separated by English colon:, and it is recommended that the key names be added with English double quotes "" to facilitate parsing in different languages.
The commonly used data types inside JSON are nothing more than strings, numbers, Boolean, dates, and null. Strings must be enclosed in double quotes, and the rest are not used. The date type is quite special, so I won’t go into details here. It is recommended that if the client does not require the function of sorting by date, then just pass the date and time directly as a string, which can save a lot of trouble.
JSON Example
// 描述一个人 var person = { "Name": "Bob", "Age": 32, "Company": "IBM", "Engineer": true } // 获取这个人的信息 var personAge = person.Age; // 描述几个人 var members = [ { "Name": "Bob", "Age": 32, "Company": "IBM", "Engineer": true }, { "Name": "John", "Age": 20, "Company": "Oracle", "Engineer": false }, { "Name": "Henry", "Age": 45, "Company": "Microsoft", "Engineer": false } ] // 读取其中John的公司名称 var johnsCompany = members[1].Company; // 描述一次会议 var conference = { "Conference": "Future Marketing", "Date": "2012-6-1", "Address": "Beijing", "Members": [ { "Name": "Bob", "Age": 32, "Company": "IBM", "Engineer": true }, { "Name": "John", "Age": 20, "Company": "Oracle", "Engineer": false }, { "Name": "Henry", "Age": 45, "Company": "Microsoft", "Engineer": false } ] } // 读取参会者Henry是否工程师 var henryIsAnEngineer = conference.Members[2].Engineer;
That’s all about JSON. For more details, please refer to the information for in-depth study during the development process.
What is JSONP?
First let’s talk about how JSONP came into being:
In fact, there are many explanations about JSONP on the Internet, but they are all the same and confusing. It is a bit difficult for many people who are new to it to understand. It is not a small thing, try it. Try to explain the problem in your own way and see if it helps.
A well-known problem, Ajax direct request for ordinary files has the problem of cross-domain unauthorized access. Regardless of whether you are a static page, dynamic web page, web service, or WCF, as long as it is a cross-domain request, it is not allowed;
But we We also found that when calling js files on a Web page, it is not affected by whether it is cross-domain (not only that, we also found that all tags with the "src" attribute have cross-domain capabilities, such as