118-(AJAX)get

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>get</title>
</head>
<body>
	<input type="text" name="" placeholder="请输入城市名称">
	<button>获取城市天气</button>

	<div class="content">
		<h2></h2>
		<img src="">
		<span></span>
	</div>

	<script type="text/javascript" src="./ajax.js"></script>
	<script type="text/javascript">
		var xhrUserList = new XMLHttpRequest();
		xhrUserList.open('GET', 'http://124.223.98.197:10086/prod-api/system/user/list?pageNum=1&pageSize=10');

		xhrUserList.setRequestHeader('Content-type', 'application/json');
		// xhrUserList.setRequestHeader('Authorization', 'Bearer ' + globalToken);
		xhrUserList.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('token'));

		xhrUserList.send();
		xhrUserList.onreadystatechange = function() {
			if (xhrUserList.readyState === 4) {
				var userList = JSON.parse(xhrUserList.responseText);
				if (userList.code === 200) {
					console.log(userList);
				}
			}
		}



		var urlRes = getParamsObj(location.href);
		console.log('地址栏上的参数', urlRes);
		if (urlRes) {
			alert('您好' + urlRes.name + ', 欢迎登录');
		}
		// 1.获取页面元素
		var btn = document.querySelector('button');
		var img = document.querySelector('img');
		var span = document.querySelector('span');
		// 2. 添加点击事件
		btn.onclick = function() {

			var cityName = document.querySelector('input');
			// 3.1 创建 Ajax 异步对象
			var xhr = new XMLHttpRequest();
			// 3.2 与服务器建立连接
			if(cityName.value) {
				xhr.open('get', 'https://geoapi.qweather.com/v2/city/lookup?location=' + cityName.value + '&key=bc89ee9752c9420a90ef784b04ff328e' );

				// xhr.setRequestHeader('Content-type', 'application/json'); // x-www-form-urlencoded
				xhr.send();
				xhr.onreadystatechange = function() {
					if (xhr.readyState === 4) {
						var responseObj = JSON.parse(xhr.responseText);
						if (responseObj.code == 200) {
							var cityId = responseObj.location[0].id;
							console.log('获取到青岛市的 id', cityId);


							// 'https://devapi.qweather.com/v7/weather/now?location=' + cityId + '&key=bc89ee9752c9420a90ef784b04ff328e' 
							var xhr1 = new XMLHttpRequest();
							xhr1.open('get', 'https://devapi.qweather.com/v7/weather/now?location=' + cityId + '&key=bc89ee9752c9420a90ef784b04ff328e');
							// xhr1.open('get', `https://devapi.qweather.com/v7/weather/now?location=${cityId}&key=bc89ee9752c9420a90ef784b04ff328e`);
							xhr1.send();
							xhr1.onreadystatechange = function() {
								if (xhr1.readyState === 4) {
									var resObj = JSON.parse(xhr1.responseText);
									if (resObj.code == 200) {
										console.log(resObj); // 数字 相当于icon图片的名字   101.png
										// img.setAttribute('src', './128/' + resObj.now.icon + '.png');
										img.setAttribute('src', `./128/${resObj.now.icon}.png`); // 模板字符串
										span.innerText = resObj.now.text
									}
								}
							}
							var getWUrl = 'https://devapi.qweather.com/v7/weather/now?location=' + cityId + '&key=bc89ee9752c9420a90ef784b04ff328e';
							var obj = getWeatherInfo('get', getWUrl, '');
							console.log('图片', obj);
						}
					}
				}
			} else {
				alert('请输入城市名称');
			}
			
		}

		// btn.onclick = function() {
		// 	var cityName = document.querySelector('input');
		// 	if (!cityName.value) {
		// 		alert('请输入城市名称');
		// 		return;
		// 	}
		// 	var getIdUrl = 'https://geoapi.qweather.com/v2/city/lookup?location=' + cityName.value + '&key=bc89ee9752c9420a90ef784b04ff328e';
		// 	var responseObj = getWeatherInfo('get', getIdUrl, '');
		// 	console.log('-----------', responseObj); // 异步回调, 不能等待函数结果返回就执行后面的代码了
		// }

		function getWeatherInfo(method, url, param) { // param string
			console.log('进入方法');
			// 3.1 创建 Ajax 异步对象
			var xhr = new XMLHttpRequest();
			// 3.2 与服务器建立连接
			xhr.open(method, url);
			xhr.setRequestHeader('Content-type', 'application/json');
			xhr.send(method === 'get' ? '' : param);
			xhr.onreadystatechange = function() {
				if (xhr.readyState === 4) {
					var responseObj = JSON.parse(xhr.responseText);
					if (responseObj.code == 200) {
						console.log('获取到最终结果', responseObj)
						return responseObj;
					}
				}
			}
		}
	</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七色的天空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值