开发者

javascript send one way message to php

How can I use javascript to send a one way message to php? I would like to get the browser information from javascript and just send it to php in the background. I know I can get some of this from php, but I'd rather 开发者_开发问答use javascript. Is there a way to do this without a framework like jquery?


Yes, you can do it with something like this:

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari

    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
       // IE
       self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.onreadystatechange = function() {

        if (self.xmlHttpReq.readyState == 4) {
            alert('Here goes something');
            self.xmlHttpReq.send('browser info here');
        }
    }
} 

This will send "browser info here" as POST in the php page you pass to the function as url. I didnt test it though


You would have to submit an AJAX request to a PHP script. Yes, you could do it without using a framework but I wouldn't advise it.


You need to make an AJAX call to a PHP page, preferably using POST. Any data you want to send needs to be sent along with the request.

I recommend using a framework such as jQuery, but if you insist on using raw JavaScript, you want to research XMLHttpRequest.


// fix for older IE versions
// see http://blogs.msdn.com/b/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
if( typeof window.XMLHttpRequest === 'undefined' &&
    typeof window.ActiveXObject === 'function') {
    window.XMLHttpRequest = function() {
        try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
        try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
        return new ActiveXObject('Microsoft.XMLHTTP');
    };
}

function postData(url, data, errhandler) {
    var req = new XMLHttpRequest;
    req.onreadystatechange = function() {
        if(this.readyState === 4 && this.status !== 200 && errhandler)
            errhandler(this);
    };
    try {
        req.open('POST', url, true); // async post request
        req.send(data);
    }
    catch(e) {
        if(errhandler)
            errhandler(req);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜