When it comes to Ajax, it inevitably faces two problems, the first of which is what format does AJAX exchange data for ? The second is how do cross-domain requirements work ? Both problems now have different solutions, such as data can be described by a custom string or XML, and cross-domain can be resolved by a server-side proxy. The preferred solution so far is to use JSON to pass data across domains by JSONP. JSON is a data interchange format, and Jsonp is an unofficial cross-domain data interaction protocol.
JSON is a type of text-based data exchange, or a data description format.
The advantages of JSON:
1, based on plain text, cross-platform transmission is extremely simple;
2, JavaScript native support, background language almost all support;
3, lightweight data format, occupies very few characters, especially suitable for internet transmission;
4, readability is strong, although less than the XML so at a glance, but in a reasonable order after indentation is easy to identify;
5, easy to write and analysis, of course, if you want to know the data structure;
The format or rule of JSON:
JSON can describe a data structure in a very simple way, and XML can do it all, so it's completely pitches in cross-platform terms.
1. JSON has only two data type descriptors, curly braces {} and square brackets [], the remaining English colons: is the map, comma, is the delimiter, the English double quotation mark "" is the definition.
2, curly braces {} are used to describe a set of "different types of unordered key-value pairs" (each key-value pair can be interpreted as an attribute description of OOP), and square brackets [] are used to describe a set of "ordered sets of data of the same type" (which corresponds to an array of OOP).
3, if there are more than one of the two sets of children, it is separated by commas.
4. The key value pairs are separated by a colon: and the key name is suggested with the English double quotation mark "" to facilitate the parsing of different languages.
5, JSON internal data type is nothing more than string, number, Boolean, date, null so several, the string must be enclosed in double quotation marks, the rest are not, the date type is special, here does not expand the narrative, but it is recommended if the client is not sorted by date function requirements, It would be nice to pass the date and time directly as a string, which would save a lot of trouble.
JSONP (JSON with Padding) is a "usage pattern" of JSON that can be used to solve the problem of cross-domain data access for mainstream browsers. Because of the same-origin policy, Web pages that are generally located in server1.example.com cannot communicate with servers that are not server1.example.com, while HTML <script> elements are an exception. Using this open strategy of <script>, the Web page can get JSON data that is dynamically generated from other sources, and this usage pattern is called JSONP. The data captured with JSONP is not JSON, but arbitrary JavaScript, executed with a JavaScript interpreter instead of parsing with a JSON parser
Client
<meta content= "text/html; Charset=utf-8 "http-equiv=" Content-type "/> <script type=" Text/javascript ">functionJsonpcallback (Result) {//alert (result); for(varIinchresult) {Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc. } } varJsonp=document.createelement ("Script"); Jsonp.type= "Text/javascript"; JSONP.SRC= "Http://crossdomain.com/services.php?callback=jsonpCallback"; document.getElementsByTagName ("Head") [0].appendchild (JSONP); </script>
Server-side
<? PHP // The service side returns JSON data $arr=array(' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5); $result=json_encode ($arr); // echo $_get[' callback ']. ' ("hello,world!") '; Echo $_get[' callback ']. " ($result) "; Dynamic execution callback function $callback=$_get[' Callback ']; Echo $callback. " ($result) ";
JSONP is a powerful technology for building mashups, but unfortunately it is not a panacea for all cross-domain communication needs. It has some drawbacks that must be carefully considered before submitting development resources.
first, and most important, there is no error handling on the JSONP call. if the dynamic script insert is valid, the call is executed, and if it is not valid, the silence fails. There is no hint of failure. For example, you cannot catch a 404 error from the server, and you cannot cancel or restart the request.
Another major drawback of JSONP is the danger of being used by untrusted services. because the JSONP service returns a JSON response packaged in a function call, the function call is executed by the browser, which makes the host WEB application more susceptible to various types of attacks. If you intend to use the JSONP service, it is important to understand the threats it can pose.
The difference between JSONP and Ajax
1, Ajax and JSONP the two technologies in the invocation of the way "looks" very much like, the purpose is the same, is to request a URL, and then the server to return the data processed, so the jquery and ext and other frameworks have Jsonp as a form of Ajax encapsulation;
2, but Ajax and JSONP are actually different things in nature. The core of Ajax is to obtain non-page content through XMLHttpRequest, while the core of JSONP is dynamic add <script> tag to invoke the JS script provided by the server.
3, so that, in fact, the difference between Ajax and JSONP is not whether cross-domain, AJAX through the service-side proxy can be implemented across domains, JSONP itself does not exclude the same domain data acquisition.
4, there is, Jsonp is a way or non-mandatory protocol, like Ajax, it does not have to use JSON format to pass the data, if you want to, the string is OK, but this is not conducive to the use of JSONP to provide public services.
JSON and JSONP