• 记录ajax的jsonp类型请求


    常规的ajax请求,可以直接外层return后,就为异步,但是ajax的jsonp类型请求,直接外层return不为异步,得不到请求promiss结果
    正常的ajax:

    export function _fp (){
        // return new Promise(((resolve, reject) => {
        //
        // }))
        // return Promise.reject({success:true,message:'请求成功'});
        // return getBanner().then(res=>{
        //     return Promise.resolve({success:true,message:'获取菜单信息成功'});
        // })
        return $.ajax({
            // dataType: 'jsonp',
            // async : true,//TODO:这个太重要了
            url:'/api/yongfeng_port/index.php/home/index/banner',
            method:'get',
            success:function (res){
                console.log(res)//走这里
            },
            error:function (err){
                console.log(JSON.parse(err.responseText))
            }
        })
    }
     _fp().then(res=>{
            console.log(res)//这里得到结果
        })
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    jasop的

    $.ajax({
                    type: 'GET',
                    dataType: 'jsonp',
                    async : true,//TODO:这个太重要了
                    timeout:2000,//TODO:这个太重要了,jsonp这个格式比较特殊  不走error,加个这个就行
                    data: {
                        platformSet: true,
                        ticket: data,
                        service: location.href
                    },
                    url: process.env.VUE_APP_ZHXY_SERVER + '/desktop/backend/api/portal/navbarConfig',
                    success: function (res) {
                        if (res.user) {
                            return resolve({success:true,message:'获取导航栏用户信息成功',user:res.user,navData:res})
                        } else {
                            return reject({success:false,message:'获取导航栏用户信息失败'})
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        return reject({success:false,message:'获取导航栏用户信息失败'})
                    }
                })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    和数链BaaS化服务体系赋能企业数字经济升级发展
    YOLOv3: An Incremental Improvement 论文详细解读
    编程大杂烩(三)
    20231013比赛总结
    css动画基本使用
    Day05-Python组合数据类型
    java计算机毕业设计红河旅游信息服务系统源程序+mysql+系统+lw文档+远程调试
    JAVASE语法零基础——封装与包
    一文教会你如何用 Python 分割合并大文件
    Python模块和包
  • 原文地址:https://blog.csdn.net/weixin_44524243/article/details/126285693