It's all about
connecting the dots

原生JS写的Ajax封装函数

I forgot the source of the following code snippet, maybe it is from a book named Learning from jQuery


var ajax = function(url, fnSuccess, fnFailed) {
	//create oAjax object
	var oAjax;
	if(window.XMLHttpRequest) {
		oAjax = new XMLHttpRequest();
	}else {
		oAjax = new ActiveXObject("Microsoft.XMLHTTP");
	}

	//connect to the server
	oAjax.open("GET", url, true); //open("POST" or "GET" method, url, 是否异步加载)

	//send the request
	oAjax.send();

	//receive the reply from the server side
	oAjax.onreadystatechange = function() {
		if (oAjax.readyState ==4) { //4 indicates job finished
			if (oAjax.status == 200) { //200 indicates job finished successfully 
				fnSuccess(oAjax.responseText);
			} else{
				if (fnFailed) {
					fnFailed();
				};
			};
		};
	}
}
赞(0) 打赏
版权声明:非商业用途转载请注明文章链接,商业用途转载请联系邮箱获取授权。
文章名称:《原生JS写的Ajax封装函数》
文章链接:https://www.orzzone.com/%e5%8e%9f%e7%94%9fjs%e5%86%99%e7%9a%84ajax%e5%b0%81%e8%a3%85%e5%87%bd%e6%95%b0.html
商业联系:yakima.public@gmail.com
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏