zoukankan      html  css  js  c++  java
  • ajax跨域请求获取jsonp数据

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="jquery.js"></script>
    <script type="text/javascript">
    function getIntface(){
    //?cityname=北京&key=e4bc21879a649b8db8ed5a6ed81e3610&dtype=json
    $.ajax({
    type : "get",
    async:false,
    url:"http://op.juhe.cn/onebox/weather/query",
    data:{"cityname":"北京","key":"e4bc21879a649b8db8ed5a6ed81e3610","dtype":"json"},
    dataType : "jsonp",
    jsonp: "jsoncallback",
    jsonpCallback:"success_jsonpCallback",
    success : function(json){
    alert(json);
    }
    });
    }
    function getIntface2(){
    var url = "http://op.juhe.cn/onebox/weather/query";
    var data = {"cityname":"北京","key":"e4bc21879a649b8db8ed5a6ed81e3610","dtype":"json"};
    $.ajax({
    type : "get",
    async : false, //同步请求
    url : url,
    data : data,
    dataType : "jsonp", //跨域请求需要使用jsonp
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    timeout:1000,
    success:function(json){
    alert(json.reason); //请求成功前台给出提示
    alert(json.result.data.realtime.city_code);
    },
    error: function() {
    alert("失败!");
    }
    });
    }
    </script>
    </head>
    <body>
    <input type="submit" value='load' onclick="getIntface()">
    <input type="submit" value='load' onclick="getIntface2()">


    </body>
    </html>

  • 相关阅读:
    RDay2-Problem 2 B
    杭电 1862 EXCEL排序(sort+结构体)
    杭电 2803 The MAX(sort)
    杭电 5053 the Sum of Cube(求区间内的立方和)打表法
    杭电 2089 不要62
    杭电 4548 美素数(素数打表)
    杭电2098 分拆素数和
    杭电1722 Cake (分蛋糕)
    素数判定 (素数打表)
    最小公倍数
  • 原文地址:https://www.cnblogs.com/milude0161/p/6022592.html
Copyright © 2011-2022 走看看