概述1、傳統(tǒng)的Web應(yīng)用
2、AJAX
“偽”AJAX由于HTML標簽的iframe標簽具有局部加載內(nèi)容的特性,所以可以使用其來偽造Ajax請求。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <div> <p>請輸入要加載的地址:<span id="currentTime"></span></p> <p> <input id="url" type="text" /> <input type="button" value="刷新" onclick="LoadPage();"> </p> </div> <div> <h3>加載頁面位置:</h3> <iframe id="iframePosition" style="width: 100%;height: 500px;"></iframe> </div> <script type="text/javascript"> window.onload= function(){ var myDate = new Date(); document.getElementById('currentTime').innerText = myDate.getTime(); }; function LoadPage(){ var targetUrl = document.getElementById('url').value; document.getElementById("iframePosition").src = targetUrl; } </script> </body> </html>
原生AJAXAjax主要就是使用 【XmlHttpRequest】對象來完成請求的操作,該對象在主流瀏覽器中均存在(除早起的IE),Ajax首次出現(xiàn)IE5.5中存在(ActiveX控件)。 1、XmlHttpRequest對象介紹 XmlHttpRequest對象的主要方法:
View CodeXmlHttpRequest對象的主要屬性:
View Code2、跨瀏覽器支持
IE7+, Firefox, Chrome, Opera, etc.
IE6, IE5
基于原生Ajax-demo原生ajax發(fā)送post請求要帶上請求頭
jQuery AjaxjQuery其實就是一個JavaScript的類庫,其將復雜的功能做了上層封裝,使得開發(fā)者可以在其基礎(chǔ)上寫更少的代碼實現(xiàn)更多的功能。
注:2.+版本不再支持IE9以下的瀏覽器
Jquery Ajax-方法
基于JqueryAjax -demo通過ajax返回得到的字符串,可以通過 跨域AJAX由于瀏覽器存在同源策略機制,同源策略阻止從一個源加載的文檔或腳本獲取或設(shè)置另一個源加載的文檔的屬性。 特別的:由于同源策略是瀏覽器的限制,所以請求的發(fā)送和響應(yīng)是可以進行,只不過瀏覽器不接受罷了。 瀏覽器同源策略并不是對所有的請求均制約:
跨域,跨域名訪問,如:http://www. 域名向 http://www.域名發(fā)送請求。 1、JSONP實現(xiàn)跨域請求 JSONP(JSONP - JSON with Padding是JSON的一種“使用模式”),利用script標簽的src屬性(瀏覽器允許script標簽跨域) -- localhost:8889 :
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("func([11,22,33,44])")
*******************************************
-- localhost:8888 :
function Jsonp1(){
var tag = document.createElement('script');
tag.src = 'http://localhost:8889/index';
document.head.appendChild(tag);
document.head.removeChild(tag);
}
function func(arg) {
console.log(arg)
}
接著執(zhí)行了 -- jQuery 實現(xiàn)方式:
改進版: -- localhost:8889 :
class MainHandler(tornado.web.RequestHandler):
def get(self):
callback = self.get_argument('callback')
self.write("%s([11,22,33,44])"%callback)
此時頁面中,訪問:
http://localhost:8889/index?callback=xxoo
http://localhost:8889/index?callback=ssss
都可以!
*******************************************
-- localhost:8888 :
function func(arg) {
console.log(arg)
}
function jsonpclick() {
$.ajax({
url:'http://localhost:8889/index',
dataType:'jsonp',
jsonp:'callback',
jsonpCallback:'func',
});
}
代碼中相當于發(fā)送了: http://localhost:8889/index?callback=func
實例: 江西電視臺節(jié)目單獲取2、CORS 隨著技術(shù)的發(fā)展,現(xiàn)在的瀏覽器可以支持主動設(shè)置從而允許跨域請求, 即:跨域資源共享(CORS,Cross-Origin Resource Sharing) 其本質(zhì)是設(shè)置響應(yīng)頭,使得瀏覽器允許跨域請求。 * 簡單請求 OR 非簡單請求
* 簡單請求和非簡單請求的區(qū)別?
* 關(guān)于“預(yù)檢”
基于cors實現(xiàn)AJAX請求: a、支持跨域,簡單請求
實例: function corsSimple() {
$.ajax({
url:'http://localhost:8889/index',
type:'post',
data:{'v1':'k1'},
success:function (callback) {
console.log(callback)
}
});
}
***********************************************
-- localhost:8889
def post(self, *args, **kwargs):
self.set_header('Access-Control-Allow-Origin', "http://localhost:8888")
v1 = self.get_argument('v1')
print(v1)
self.write('--post--')
b、支持跨域,復雜請求 由于復雜請求時,首先會發(fā)送“預(yù)檢”請求,如果“預(yù)檢”成功,則發(fā)送真實數(shù)據(jù)。
--localhost:8889 def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Methods', "PUT") self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") print('--option--') def put(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") print('--put--') self.write('--put--') *********************************************** --localhost:8888 function corscomplex() { $.ajax({ url:'http://localhost:8889/index', type:'put', data:{'v1':'k1'}, success:function (callback) { console.log(callback) } }); } 如果客戶端,加上了自定義請求頭,服務(wù)器端要加上
實例: --localhost:8889 def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Methods', "PUT") self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") self.set_header('Access-Control-Allow-Headers', "key1,key2") self.set_header('Access-Control-Max-Age', 10) print('--option--') def put(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") print('--put--') self.write('--put--') *********************************************** --localhost:8888 function corscomplex() { $.ajax({ url:'http://localhost:8889/index', type:'put', headers:{'key1':'xxx'}, data:{'v1':'k1'}, success:function (callback) { console.log(callback) } }); } 控制預(yù)檢過期時間:
c、跨域傳輸cookie 在跨域請求中,默認情況下,HTTP Authentication信息,Cookie頭以及用戶的SSL證書無論在預(yù)檢請求中或是在實際請求都是不會被發(fā)送。 如果想要發(fā)送:
--localhost:8889 def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Methods', "PUT") self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") self.set_header('Access-Control-Allow-Credentials', "true") //必須 print('--option--') def put(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") self.set_header('Access-Control-Allow-Credentials', "true") //必須print(self.cookies) self.set_cookie('k1','kkk') self.write('--put--') *********************************************** --localhost:8888 function corscomplex() { $.ajax({ url:'http://localhost:8889/index', type:'put', data:{'v1':'k1'}, xhrFields:{withCredentials: true}, success:function (callback) { console.log(callback) } }); } d、跨域獲取響應(yīng)頭 默認獲取到的所有響應(yīng)頭只有基本信息,如果想要獲取自定義的響應(yīng)頭,則需要再服務(wù)器端設(shè)置Access-Control-Expose-Headers。 --localhost:8889 def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Methods', "PUT") self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") self.set_header('Access-Control-Allow-Headers', "key1,key2") self.set_header('Access-Control-Allow-Credentials', "true") print('--option--') def put(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "http://localhost:8888") self.set_header('Access-Control-Allow-Credentials', "true") self.set_header('bili', "daobidao") //設(shè)置響應(yīng)頭 self.set_header('Access-Control-Expose-Headers', "xxoo,bili") //允許發(fā)送 print(self.cookies) self.set_cookie('k1','kkk') self.write('--put--') *********************************************** --localhost:8888 function corsRequest() { $.ajax({ url:'http://localhost:8889/index', type:'put', data:{'v1':'k1'}, xhrFields:{withCredentials: true}, success:function (callback,statusText, xmlHttpRequest) { console.log(callback); console.log(xmlHttpRequest.getAllResponseHeaders()); } }); } 示例代碼整合:
localhost:8888/index.html
localhost:8889/python
|
|
|
來自: highoo > 《數(shù)據(jù)分析》