【问题标题】:Get result from php file without usig jquery在不使用 jquery 的情况下从 php 文件中获取结果
【发布时间】:2012-01-30 19:27:14
【问题描述】:

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

【问题讨论】:

  • 您的意思是对 php 输出进行 ajax 调用以进行显示吗?当然可以,jQuery 只是一个让你更轻松的框架,你会被困在自己编写 ajax 的代码中,但你总是可以只搜索已经制定的解决方案。
  • @Jakub 是的,我已经搜索过了,但我需要 jQuery .get() 之类的东西,我不能使用很多代码,我需要一个小程序
  • 有一次我有个老板,他也要求不要使用太多的库以保持页面的小。现在我每周六教他 JavaScript ))。问题是当您不使用已经建立的库时,您必须编写更多自定义 JavaScript。如果您使用某些库,这最终会导致更大的大小。这也可能导致库中已经处理的错误。

标签: javascript jquery ajax get


【解决方案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
            }
        }
    }
}

日期例如是“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');

【讨论】:

  • 谢谢!但我后来看到了。我需要一个小程序或小技巧来在我的页面正文部分使用
  • 你能给我一个例子,代码应该做什么吗?我想我可以给你答案。
  • 好的,比如<script>someobject.ajax('http://muhost.com/log.php?ip=x.x.x.x.&url=http://blah.com&12:22PM')</script>
  • 我可以在
  • 或者完全我只需要打开这个网址而不注意它的结果http://muhost.com/log.php?ip=x.x.x.x.&url=http://blah.‌​com&12:22PM'
【解决方案2】:

我用这个用javascript发送请求,它完美地工作:

function httpGet(theUrl)
{
        var xmlHttp = null;

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

和html代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多