jquery跨域请求

2019-12-20  本文已影响0人  朦胧之月

function getData(opt){

    var url = opt.url,

        type = opt.type || "GET",

        data = opt.data || {},

        success = opt.callback || null;

    if (type === 'POST') {   // post请求跨域

        var baseUrl = 'http://test.hccb.cc/publicManage';

        $.ajax({

            async: false,

            type: type,

            url: baseUrl + url,

            headers: {

                "Content-Type": "application/json;charset=utf8"

            },

            data:  JSON.stringify(data),

            dataType: 'json',

            crossDomain: true,

            success: function(data){

                    success && success(data);

            },

            fail: function(err){

                console.log(err);

            }

        });

    } else {   // get请求跨域

        $.ajax({

            async: false,

            type: type,

            url: url,

            data: data,

            dataType: 'jsonp',

            success: function(data){

                if(data.message == 'false'){         

                    window.location.href = data.data+"?url=";

                } else {

                    success && success(data.data);

                }

            },

            fail: function(err){

                console.log(err);

            }

        });

    }

}

上一篇 下一篇

猜你喜欢

热点阅读