奈飞|Ajax跨域请求的两种实现方式( 二 )



    static getMessageKey() {
        let message_key = FrameHttp.message_key
        FrameHttp.message_key = (FrameHttp.message_key+1)%FrameHttp.message_key_max 
        return message_key
    

    static ajax(arg) {
        if (FrameHttp.isInit) {
            // console.log(arg)
            const { success error ...others  = arg
            let message_key = FrameHttp.getMessageKey()
            FrameHttp.request_map[message_key
 = { success error 
            FrameHttp.frame.contentWindow.postMessage({
                cmd: FrameHttpCmd.REQUEST
                data: others
                key: message_key
             '*')
            console.log('(INFO)FrameHttp.ajax' others)
        
        else {
            FrameHttp._request_buffer.push(arg)
            console.log('(INFO)FrameHttp.ajax:push buffer')
        
    

    static onMessage(e) {
        const { data  = e
        if (data.cmd == FrameHttpCmd.INIT) {
            FrameHttp._initFrame()
        
        else if (data.cmd == FrameHttpCmd.REQUEST_CALLBACK) {
            // console.log(data.key data.success)
            let item = FrameHttp.request_map[data.key

            if (data.error) {
                if (item.error) {
                    item.error(data.error)
                
            
            else {
                if (item.success) {
                    item.success(data.success)