当前位置 : 主页 > 网络编程 > JavaScript >

原生ajax实现后台数据交互

来源:互联网 收集:自由互联 发布时间:2021-06-28
a.js window.onload = function() {function loadXMLDoc(){var xhr;//浏览器兼容性判断if(window.XMLHttpRequest){//code for IE7+,Firefox,Chrome,Opera,Safarixhr = new XMLHttpRequest();}else{//code for IE5,IE6xhr = new ActiveXObject("Micros
a.js
window.onload = function() {
			function loadXMLDoc()
			{
				var xhr;
				//浏览器兼容性判断
				if(window.XMLHttpRequest)
				{//code for IE7+,Firefox,Chrome,Opera,Safari
					xhr = new XMLHttpRequest();
				}
				else
				{//code for IE5,IE6
					xhr = new ActiveXObject("Microsoft..XMLHTTP");

				}
				xhr.onreadystatechange = function() {
					if(xhr.readyState == 4 && xhr.status == 200) {
					   var data = JSON.parse(xhr.responseText);
					//    console.log(data.prodline);
					for(var i = 0;i < data.length;i++) {
						console.log(data[i].prodline);
						// 从数据库获取prodline添加到selectBox;
						var sel=document.getElementById("Prodline-sel");
						var option = new Option(data[i].prodline);
						sel.options.add(option);
					}
					
					}
				}
				xhr.open("GET",'url',true);
				xhr.send();
			}
			loadXMLDoc();
}
网友评论