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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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');
回答2:
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>