不太熟悉XMLHttpRequest
,但我在Google Chrome擴展中使用了跨源功能。這很好(我可以確認我得到了我需要的適當數據),但我似乎無法將它存儲在「響應」變量中。將XMLHttpRequest.responseText存儲到變量中
我很感激任何幫助。
function getSource() {
var response;
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
response = xmlhttp.responseText;
//IM CORRECTLY SET HERE
}
//I'M ALSO STILL WELL SET HERE
}
//ALL OF A SUDDEN I'M UNDEFINED.
xmlhttp.open("GET","http://www.google.com",true);
xmlhttp.send();
return response;
}
ur在你實際收到響應之前返回響應 –