代码:
<!DOCYPE html>
<html>
<head>
<title>Ajax Test</title>
<script src = "myAjaxLib.js">
</script>
<script>
function cback(text){
alert(text);
}
window.onload = function(){
document.getElementById("btn1").onclick = function(){
doAjax("libtest.php", "param=hello", "cback", "GET", 0);
}
}
</script>
</head>
<body>
<input type="button" id="btn1" value="Make call" />
</body>
</html>
myAjaxLib.js代码
function getXMLHttpRequest(){
try{
try{
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
return new ActiveXObject("Msxml2.XMLHTTP");
}
}
catch(e){
return new XMLHttpRequest();
}
}
function doAjax(url, query, callback, reqtype, getxml){
var myreq = getXMLHttpRequest();
myreq.onreadystatechange = function(){
if(myreq.readyState==4){
if(myreq.status ==200){
var item = myreq.responseText;
if(getxml == 1) item = myreq.responseXML;
eval(callback + '(item)');
}
}
}
if(reqtype.toUpperCase() == "POST"){
requestPOST(url, query,myreq);
}
else
{
requestGET(url, query, myreq);
}
}
function requestGET(url, query, req){
var myRandom = parseInt(Math.random()*99999999);
if(query == ''){
var callUrl = url + '?rand='+ myRandom;
}else{
var callUrl = url + '?' + query + '&rand=' + myRandom;
}
req.open("GET", callUrl, true);
req.send(null);
}
function requestPOST(url, query, req){
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(null);
}
libtest.php代码
<?
echo "Parameter value was: ".$_GET['param'];
?>
运行结果出错:
XMLHttpRequest cannot load file:///H:/javascript/libtest.php?param=hello&rand=91751162. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.requestGET @ myAjaxLib.js:44doAjax @ myAjaxLib.js:32document.getElementById.onclick @ 17.2.html:13
myAjaxLib.js:44 Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///H:/javascript/libtest.php?param=hello&rand=91751162'.
这到底是什么原因,求好心人解答