首页 > 技术文章 > jsonp跨域请求

Neroi 2019-03-05 13:59 原文

原生XHR:

    function submitJsonp2() {
        var tag = $('<script>');
        tag.attr('src', 'http://127.0.0.1:8000/an/?callback=fuck');//发送给别的服务器自己的回调函数名
        $(document.head).append(tag);
        $(document.head).children(tag).remove();
    }

在页面用ajax跨域:

    function fuck(arg) {//定义回调函数
        $('#content').empty().append(arg);
    }
    function submitJsonp3() {
        $.ajax({
            url: 'http://127.0.0.1:8000/an/',
            type: "GET",
            dataType: 'jsonp',
            jsonp:'callback',
            jsonpCallback:'fuck'//指定回调函数
        })
    }

 

推荐阅读