Home > Web Front-end > JS Tutorial > body text

Native JS and jQuery use jsonp respectively to obtain 'current weather information'

不言
Release: 2018-07-05 17:44:42
Original
1729 people have browsed it

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>
Copy after login

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          : &#39;get&#39;,
                async         : false,
                url           : &#39;http://api.k780.com/?app=weather.today&weaid=412&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=data&#39;,
                dataType      : &#39;jsonp&#39;,
                jsonp         : &#39;callback&#39;,
                jsonpCallback : &#39;data&#39;,
                success       : function(data){                    if(data.success!=&#39;1&#39;){
                        alert(data.msgid+&#39; &#39;+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(&#39;fail&#39;);
                }
            });
        });    </script></head><body></body></html>
Copy after login

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

Convert URL to JSON format

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template