我知道这个问题现在真的很老了,但是我在尝试将页面内容放入变量时遇到了同样的问题,但最终在 Javascript 中找到了一种方法:D(在互联网的帮助下.. .)
就这样吧……
我制作了一个带有回调的函数来获取所需的页面:
function getPageContents(callback,url,params) {
http=new XMLHttpRequest();
if(params!=null) {
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
} else {
http.open("GET", url, true);
}
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
callback(http.responseText);
}
}
http.send(params);
}
请注意,我以这种方式制作它,它不会接受 GET 参数。这是故意的,因为我不需要为我的应用程序使用 GET。如果设置了参数,这些将作为 POST 发送。
然后要使用该函数,假设我想向findpersoninfo.php 发布一个名称,它将输出该人员信息的 JSON 数组,我可以这样做:
getPageContents(function(result) {
personinfo=JSON.parse(result);
//Now I can do anything here with the personinfo array
},'http://localhost/findpersoniinfo.php','fname=stretch&lname=wright')
更进一步,你可以将它嵌套在另一个函数中,我们称之为getPersonInfo():
function getPersonInfo(fname,lname) {
getPageContents(function(result) {
personinfo=JSON.parse(result);
//Now I can do anything here with the personinfo array
},'http://localhost/findpersoninfo.php','fname='+fname+'&lname='+lname)
}
当然,我对 Javascript 的了解还处于起步阶段,欢迎任何有建设性的反馈:D