这可以在不使用jQuery的情况下从php文件获得结果吗?我没有权限使用Jquery和任何其他javaScript平台。
答案 0 :(得分:4)
以下是一个例子:
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');
答案 1 :(得分:0)
我用这个用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>