原生js实现ajax方法

xiaoxiao2021-02-28  37

var Ajax={ get: function(url, fn) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { fn.call(this, xhr.responseText); } }; xhr.send(); }, post: function (url, data, fn) { var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) { fn.call(this, xhr.responseText); } }; xhr.send(data); } };
转载请注明原文地址: https://www.6miu.com/read-2626143.html

最新回复(0)