JSONP跨域处理实例

<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <title>test</title>
</head>
<body>
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script type="text/javascript">
    function success_jsonpCallback(data){
        var html = '';
        var pos = '';
        html += '<ul>';
        $.each(data, function(k, v) {
            if(k<10){
                pos = '【' + v.city+ '】' + v.positionName + '('+ v.salary +') - '+v.companyName;
                if(pos.length > 20){
                    pos = pos.substring(0,19)+'...';
                }
                html += '<li><a href="'+v.posiitonDetailUrl+'" target="_blank" title="【' + v.city+ '】' + v.positionName + '('+ v.salary +') - '+v.companyName+'">'+pos+'</a></li>';
            }
        });
        $('#lagouData').html(html);
    }

    function getLagouData() {
        jQuery.ajax({
            async:false,
            url: "http://www.lagou.com/join/listW3cplus?kd=前端开发",
            type: "GET",
            dataType: "jsonp",
            jsonpCallback: 'success_jsonpCallback',
            contentType: "application/jsonp; charset=utf-8",
            success: function(data) {
                success_jsonpCallback(data);
            }

        });
    }
    $(function() {
        getLagouData();
    })
   
</script>
<div id="lagouData"></div>
</body>
</html>

 

posted @ 2016-05-03 11:36  PLDaily  阅读(296)  评论(0编辑  收藏  举报