Get result from php file without usig jquery

2019-01-29 12:34发布

Is this possible to get result from a php file without using jQuery ? i haven't permission to use Jquery and any other javaScript platform.

2条回答
不美不萌又怎样
2楼-- · 2019-01-29 13:04

i use this one to send request with javascript and its works perfectly :

function httpGet(theUrl)
{
        var xmlHttp = null;

        xmlHttp = new XMLHttpRequest();
        xmlHttp.open( "GET", theUrl, false );
        xmlHttp.send( null );
        alert(xmlHttp.responseText);
}

and the html code :

<html>
<head>
<script type="text/javascript" src="log.js"></script>
</head>
<body>
        <a href="" onclick="httpGet('log.php?url=http://bizzare.com')">Send log</a>
</body>
</html>
查看更多
Melony?
3楼-- · 2019-01-29 13:19

Here is an example:

function C_xmlObject() {
    var xml = null;

    try { xml = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch(e) { try { xml = new ActiveXObject("MSXML2.XMLHTTP"); }
        catch(e) { try { xml = new XMLHttpRequest(); }
            catch(e) { } } }
    return xml;
}
function C_ajax(daten, url) {
    var xml = C_xmlObject();

    if(xml !== null) {
        xml.open('POST', url, true);
        xml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xml.setRequestHeader('Content-length', daten.length);
        xml.setRequestHeader('Connection', 'close');
        xml.send(daten);
        xml.onreadystatechange = function() {
            if(xml.readyState === 4) {
                // Do something
            }
        }
    }
}

daten is for example "name=1&name2=Hello".

Edit: Version with prototype:

Object.prototype.ajax = function(daten, url, toElement, attributeName) {
    var xml = null;

    try { xml = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch(e) { try { xml = new ActiveXObject("MSXML2.XMLHTTP"); }
        catch(e) { try { xml = new XMLHttpRequest(); }
            catch(e) { } } }

    if(xml !== null) {
        xml.open('POST', url, true);
        xml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xml.setRequestHeader('Content-length', daten.length);
        xml.setRequestHeader('Connection', 'close');
        xml.send(daten);
        if(toElement !== null) {
            xml.onreadystatechange = function() {
                if(xml.readyState === 4) {
                    if(attributeName === null) {
                        toElement = xml.responseText;
                    } else {
                        toElement[attributeName] = xml.responseText;
                    }
                }
            }
        }
    }
}

This should work: ({}).ajax('value1=...', 'index.php', document.getElementById('id'), 'innerHTML');

查看更多
登录 后发表回答