小编典典

从没有usig jquery的php文件中获取结果

ajax

是否可以在不使用jQuery的情况下从php文件获取结果?我无权使用Jquery和任何其他javaScript平台。


阅读 205

收藏
2020-07-26

共1个答案

小编典典

这是一个例子:

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例如是“ name = 1&name2 = Hello”。

编辑:带有原型的版本:

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;
                    }
                }
            }
        }
    }
}

这应该起作用:({})。ajax(’value1 =
…’,’index.php’,document.getElementById(’id’),’innerHTML’);

2020-07-26