在工作中我們經常會向後台請求各種資料,遇到各種問題,以自己在該開始開發時經常出現的錯誤來說明。
1、首先定義乙個物件 ajax
上面是封裝好的ajax物件,在呼叫時只需要進行下面操作var ajax = else
xhr.onreadystatechange = function
() }}}
出現下面中的問題是如何出現的:window.onload = function
() )
}
(1)沒有開啟伺服器,或者沒有再伺服器端開啟檔案,只是本地開啟
(2)有可能是伺服器端口衝突,沒有達到同源策略。
2、在ajax請求時需要給路徑後面鏈結一些引數,然後去請求資料(以post為例封裝)
然後呼叫此函式,demo如下:function
post
(opt)
post.prototype =
this.format();
},format: function
() this.postdata = params.join('&');
this.xhr();
},xhr: function
() }}}
new post(,
success:function(res)
})
原生JS封裝AJAX
今天我們來說說利用原生js封裝ajax.jquery框架的ajax方法確實很好用,但有時候我們寫的頁面需要引入多個js外掛程式,不一定哪個外掛程式就會和jquery發生衝突,導致jquery用不了了.或者頁面比較簡單,不需要加重瀏覽器的負擔,這時我們自己封裝乙個ajax就是乙個很好的辦法.將資料轉換...
js 原生ajax 封裝函式
1 開啟伺服器wampserver 2 將檔案放置在wampserver的www資料夾下 3 開啟時網頁位址列為localhost www下的位址。eg http localhost myworld 5.19ajax index.html?hbt 1495158145298 4 呼叫方式 函式為 a...
原生js實現Ajax的封裝
1.封裝函式function ajax options options.type options.type get touppercase options.datatype options.datatype json options.async options.async true var para...