写在前面 我的理解
JSON是JavaScript对象表示法,可以作为数据交换格式,通过JSON.stringify()和JSON.parse()实现JS对象和字符串之间的转换。是JavaScript的子集,并不能表示JavaScript里的所有值,支持对象、数组、字符串、无穷大数字、true、false、null,并且可以序列化JSON.stringify()(JS对象转化为字符串)和还原JSON.parse()(字符串转化为JS对象)。
在Web应用中,在没有导致Web浏览器重新加载任何窗口或窗体的内容情况下,脚本如何实现Web浏览器与服务器之间的通信?就用到Ajax这种使用脚本操控HTTP的Web应用架构,客户端从服务器“拉”数据。实现Ajax大的方式有很多种,在<iframe>(src属性)、<script>(src属性)传输协议之上能实现Ajax技术,但还要更简单的方式,就是借用浏览器都支持的XMLHttpResponse对象,它定义了用脚本操控HTTP的API,HTTP请求和响应各有自己的格式,这里也支持JSON格式编码的请求数据。
JavaScript有许多处理文档内容、显示、事件(HTML/CSS/Event)的属性和方法,为了兼容各个浏览器,从而产生了封装这些属性和方法后的框架和类库,这里,介绍最流行的jQuery,同时,jQuery也内置Ajax工具,可以实现不刷新网页的情况下实现请求响应。
JSON wiki
In computing, JSON (JavaScript Object Notation) is an open-standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the most common data format used for asynchronous browser/server communication, largely replacing XML, and is used by AJAX.
JSON is a language-independent data format. It was derived from JavaScript*, its syntax is (mostly) a subset of the language, but as of 2017 many programming languages include code to generate and parse JSON-format data. The official Internet media type for JSON is application/json. JSON filenames use the extension .json.
Douglas Crockford originally specified the JSON format in the early 2000s; two competing standards, RFC 7159 and ECMA-404, defined it in 2013.
JSON's applications:AJAX
Asynchronous JavaScript and JSON (or AJAJ) is dynamic web page methodology , but instead of XML, JSON is the data format. AJAJ is a web development technique that provides for the ability of a webpage to request new data after it has loaded into the web browser. Typically it renders new data from the server in response to user actions on that webpage. For example, what the user types into a search box, client-side code then sends to the server, which immediately responds with a drop-down list of matching database items.
The following JavaScript code is an example of** a client using XMLHttpRequest to request data in JSON format from a server**.
var my_JSON_object;
var http_request = new XMLHttpRequest();
http_request.open("GET", url, true);
http_request.responseType = "json";
http_request.onreadystatechange = function () {
var done = 4, ok = 200;
if (http_request.readyState === done && http_request.status === ok) {
my_JSON_object = http_request.response;
}
};
http_request.send(null);
XMLHttpRequest wiki
XMLHttpRequest (XHR) is an API in the form of an object whose methods transfer data between a web browser and a web server. The object is provided by the browser's JavaScript environment. Particularly, retrieval of data from XHR for the purpose of continually modifying a loaded web page is the underlying concept of Ajax design. Despite the name, XHR can be used with protocols other than HTTP and data can be in the form of not only XML,[1] but also JSON,[2] HTML or plain text.[3]
Ajax wiki
Ajax (also AJAX; asynchronous JavaScript and XML) is a set of Web development techniques using many Web technologies on the client side to create asynchronous Web applications. With Ajax, Web applications can send data to and retrieve from a server asynchronously (in the background) without interfering with the display and behavior of the existing page. By decoupling the data interchange layer from the presentation layer, Ajax allows for Web pages, and by extension Web applications, to change content dynamically without the need to reload the entire page. In practice, modern implementations commonly substitute JSON for XML due to the advantages of being native to JavaScript.
Ajax is not a single technology, but rather a group of technologies. HTML and CSS can be used in combination to mark up and style information. The DOM is accessed with JavaScript to dynamically display – and allow the user to interact with – the information presented. JavaScript and the XMLHttpRequest object provide a method for exchanging data asynchronously between browser and server to avoid full page reloads.
jQuery wiki
jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. It is free, open-source software using the permissive MIT license. Web analysis indicates that it is the most widely deployed JavaScript library by a large margin.
jQuery's syntax is designed to make it easier to** navigate a document, select DOM elements, create animations, handle events, and develop Ajax applications**. jQuery also provides capabilities for developers to create plug-ins on top of the JavaScript library. This enables developers to create abstractions for low-level interaction and animation, advanced effects and high-level, themeable widgets.
Usage
- The jQuery library is a single JavaScript file containing all of its common DOM, event, effects, and Ajax functions. It can be included within a Web page by linking to a local copy or to one of the many copies available from public servers.
<script src="jquery.js"></script>
## 2017.4.1 http://jquery.com/download/
<script src="jquery-3.2.0.min.js"></script>
- jQuery has a content delivery network (CDN)(内容分发网络) hosted by MaxCDN. Google and Microsoft host it as well.
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
## 2017.4.1 https://code.jquery.com
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>