Home >Web Front-end >JS Tutorial >Native JS and jQuery use jsonp respectively to obtain 'current weather information'
This article mainly introduces the use of jsonp by native JS and jQuery to obtain "current weather information". It has a certain reference value. Now I share it with you. Friends in need can refer to it
Required Skill points mastered:
jsonp, cross-domain correlation, etc.
The following two codes can be run directly.
1. Use native JS:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <p id="cur_year"></p> <p id="iciba_ds"></p></body></html><script type="text/javascript"> document.getElementById("cur_year").innerHTML =new Date(); function data(data){ //遍历 var description = ""; for(var i in data.result){ var property=data.result[i]; description+=i+" = "+property+"<br />"; } document.write(description); }</script><script type="text/javascript" src="http://api.k780.com:88/?app=weather.today&weaid=412&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=data"></script>
2. Use jquery:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="jquery-3.3.1.min.js"></script> <script> $(document).ready(function(){ $.ajax({ type : 'get', async : false, url : 'http://api.k780.com/?app=weather.today&weaid=412&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=data', dataType : 'jsonp', jsonp : 'callback', jsonpCallback : 'data', success : function(data){ if(data.success!='1'){ alert(data.msgid+' '+data.msg); exit; } //遍历 var description = ""; for(var i in data.result){ var property=data.result[i]; description+=i+" = "+property+"<br />"; } document.write(description); }, error:function(){ alert('fail'); } }); }); </script></head><body></body></html>
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. More related Please pay attention to the PHP Chinese website for content!
Related recommendations:
js implements simple click-to-click image loop playback
Introduction to getters and setters in JavaScript
The above is the detailed content of Native JS and jQuery use jsonp respectively to obtain 'current weather information'. For more information, please follow other related articles on the PHP Chinese website!