Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

控制台上显示的数据怎么传输到PHP端?

我想把这个在控制台显示的数据传到PHP端,该怎么写呢?

console.log('关键词:',self.kw);
console.log('搜索引擎:',self.fm);

//把参数转换为JSON格式
var getParms = function (url) {
    url = url.split('?')[1]+'';
    var data = {};
    if(url.indexOf('=')!==-1){
        var kv = url.split('&');
        if(kv.length){
            kv.forEach(function(e){
                if(e.indexOf('=')!==-1){
                    var em = e.split('=');
                    data[em[0]] = em[1];
                }
            });
        }
    }
    return data;
};
var self =  {};
//获取来源链接
var str = window.parent.document.referrer;
if(str){
    //有参数
    if(str.indexOf('?')!==-1){
        //获取参数
        self.data =  getParms(str);
    }
    //百度
    if(str.search('.baidu.com/') !==-1){
        self.kw = self.data['word'];
        self.fm = 'bd';
    //搜狗
    }else if(str.search('.sogou.com/') !==-1){
        //搜狗有几种方式
        if(self.data['query']){
            self.kw = self.data['query'];
        }
        if(self.data['keyword']){
            self.kw =self.data['keyword'];
        }
        if(str.search('.jsp?') !==-1){
            self.kw = 'wap' ;
        }
        self.fm = 'sg';
    //好搜
    }else if(str.search('.so.com/') !==-1){
        self.kw = self.data['q'];
        self.fm = '360';
    //神马
    }else if(str.search('.sm.cn/') !==-1){
        self.kw = self.data['q'];
        self.fm = 'sm';
    }else{
    //其他
        self.fm = 'ot';
    }
}
//关键词
console.log('关键词:',self.kw);
console.log('搜索引擎:',self.fm);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

通过 ajax 发到服务端不就好了

console.log = function(...args){
    var xhr = new XMLHttpRequest();
    xhr.open('post', '/logs')
    xhr.send(new URLSearchParams({args: args}))
}
//console.log(1,2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...