GitHub 博客园 Nanakon

【jsonp】

function handleResponse(response) { 
    alert("You’re at IP address " + response.ip + ", which is in " + response.city + ", " + response.region_name); 
} 

var script = document.createElement("script"); 
script.src = "http://freegeoip.net/json/?callback=handleResponse"; 
document.body.insertBefore(script, document.body.firstChild); 
function getVideoList(data) {
    console.log(data.retStr.length)
}

var script = document.createElement("script"); 
script.src = "http://video.baomihua.com/play2/interface/getmobilerefvideo.html&pagetype=listCod&vid=29203608&channelid=8&type=m&jsoncallback=getVideoList"; 
document.body.insertBefore(script, document.body.firstChild);

 

 

 

返回数据格式错误,过滤换行、引号等

中文编码

 

 

jQuery.getJSON()
如果URL包含字符串“callback=?”(或类似的参数,取决于服务器端 API 是如何定义的),这个请求被视为JSONP形式请求

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <script src="http://static01.baomihua.com/js/lib/jquery-1.4.4.min.js?t=20120926.js"></script>
    <script>
        // http://www.google.com.hk/?jsoncallback=getVideoList
        $.ajax({
            url: 'http://www.google.com.hk/',
            dataType: 'jsonp',
            jsonp: 'jsoncallback', // 给程序取值
            jsonpCallback: 'getVideoList', // 不产生随机数,接口缓存
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // XMLHttpRequest.readyState
                // XMLHttpRequest.status
                // XMLHttpRequest.statusText
                // textStatus
            },
            success: function(data) {}
        })

        // http://www.google.com.hk/?callback=jsonp1384414552005
        $.ajax({
            url: 'http://www.google.com.hk/',
            dataType: 'jsonp',
            success: function(data) {}
        })

        // http://www.google.com.hk/&jsonpcallback=getVideoList
        $.ajax({
            url: 'http://www.google.com.hk/&jsonpcallback=?',
            dataType: 'jsonp',
            jsonpCallback: 'getVideoList',
            success: function(data) {}
        })

        // http://www.google.com.hk/?jsonpccallback=jsonp1384414552006
        $.getJSON('http://www.google.com.hk/?jsonpccallback=?', function() {})
    </script>
</body>
</html>

 

 

posted on 2013-09-18 09:16  jzm17173  阅读(418)  评论(0编辑  收藏  举报

导航

轻音