JSON
object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON. It can't be called or constructed, and aside from its two method properties it has no interesting functionality of its own.
JSON.parse() 把JSON object 轉化成 javascript中的 數值類型
JSON.stringify() 剛好相反
wikijavascript
Ajax (short for asynchronous JavaScript and XML(XML只是以前名字的來歷,如今更多的是json格式的數據交換,固然也有其它數據格式)) 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(容許網頁或是web應用來動態地、異步地的交換數據). In practice, modern implementations commonly substitute JSON for XML(如今更多使用json代替xml) due to the advantages of being native to JavaScript.
html
Ajax is not a technology, but 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.java
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
GET
is used to retrieve remote data, and
POST
is used to insert/update remote data.
100——客戶必須繼續發出請求
101——客戶要求服務器根據請求轉換HTTP協議版本
200——成功
201——提示知道新文件的URL
300——請求的資源可在多處獲得
301——刪除請求數據
404——沒有發現文件、查詢或URl
500——服務器產生內部錯誤jquery
0: 請求未初始化,open()方法尚未調用web
1: 服務器鏈接已創建json
2: 請求已接收,接收到頭信息了跨域
3: 請求處理中,接收到響應主體了promise
4: 請求已完成,且響應已就緒,也就是相應已經完成了瀏覽器
An EventHandler
that is called whenever the readyState
attribute changes. The callback is called from the user interface thread.安全
request.open(method,url,asy)
requset.send(string)
若是是get請求,則參數直接拼接在url裏面了
若是是send請求,則參數須要寫在send()方法裏面
function post(){
var req = createXMLHTTPRequest();
if(req){
req.open("POST", "http://test.com/", true);
req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=gbk;");
req.send("keywords=手機");
req.onreadystatechange = function(){
if(req.readyState == 4){
if(req.status == 200){
alert("success");
}else{
alert("error");
}
}
}
}
}
function get(){
var req = createXMLHTTPRequest();
if(req){
req.open("GET", "http://test.com/?keywords=手機", true);
req.onreadystatechange = function(){
if(req.readyState == 4){
if(req.status == 200){
alert("success");
}else{
alert("error");
}
}
}
req.send(null);
}
}
同源策略限制了一個源(origin)中加載文本或腳本與來自其它源(origin)中資源的交互方式。
同源政策的目的,是爲了保證用戶信息的安全,防止惡意的網站竊取數據。
若是非同源,共有三種行爲受到限制。
Cookie老是保存在客戶端中,按在客戶端中的存儲位置,可分爲內存Cookie和硬盤Cookie。
同源政策規定,AJAX請求只能發給同源的網址,不然就報錯。
除了架設服務器代理(瀏覽器請求同源服務器,再由後者請求外部服務),有三種方法規避這個限制。
說說JSON和JSONP,也許你會豁然開朗,含jQuery用例
<script>
元素,向服務器請求JSON數據,這種作法不受同源政策限制;服務器收到請求後,
將數據放在一個指定名字的回調函數裏傳回來。
The HTTP headers that relate to CORS are:
Request headers
Response headers
CORS vs JSONP
CORS can be used as a modern alternative(現代瀏覽器替換jsonp的模式) to the JSONP pattern.
While JSONP supports only the GET request method, CORS also supports other types(支持其它請求) of HTTP requests.
Using CORS enables a web programmer to use regular XMLHttpRequest, which supports better error handling than JSONP.
On the other hand, JSONP works on legacy browsers(老式瀏覽器) which predate CORS support. CORS is supported by most modern web browsers. Also, while JSONP can cause cross-site scripting (XSS) issues where the external site is compromised, CORS allows websites to manually parse responses to ensure security.
CORS須要瀏覽器和服務器同時支持。目前,全部瀏覽器都支持該功能,IE瀏覽器不能低於IE10。
整個CORS通訊過程,都是瀏覽器自動完成,不須要用戶參與。對於開發者來講,CORS通訊與同源的AJAX通訊沒有差異,代碼徹底同樣。瀏覽器一旦發現AJAX請求跨源,就會自動添加一些附加的頭信息,有時還會多出一次附加的請求,但用戶不會有感受。
所以,實現CORS通訊的關鍵是服務器。只要服務器實現了CORS接口,就能夠跨源通訊。