在.js文件中,我可以在ajax responsetext处理后更新变量吗?

问题描述:

所以我有一个.js文件,我需要从PHP文件中检索一个变量。不,我不能让服务器把.js当成.php。在.js文件中,我可以在ajax responsetext处理后更新变量吗?

所以无论如何,我有此脚本

function getPHPVariable(){ 
var ajaxRequest; // The variable that makes Ajax possible! 

try{ 
    // Opera 8.0+, Firefox, Safari 
    ajaxRequest = new XMLHttpRequest(); 
} catch (e){ 
    // Internet Explorer Browsers 
    try{ 
     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (e) { 
     try{ 
      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e){ 
      // Something went wrong 
      alert("Your browser broke!"); 
      return false; 
     } 
    } 
} 
// Create a function that will receive data sent from the server 
ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4){ 
     variableIWant = ajaxRequest.responseText; 
    } 
} 
ajaxRequest.open("GET", "phpfile.php", true); 
ajaxRequest.send(null); 

} 

现在variableIWant是我需要的东西在另一个字符串中使用以后,但每次我把它它显示为未定义。我知道变量正在被正确发送,因为只需添加alert(variableIWant);在responseText行下面,它正确地提醒我该变量。

所以为了简单起见,是否有可能获得变量IWant并将其用于另一个字符串,或者我是SOL,因为它必须等待readystate?

+0

“我不能让服务器把.js当成.php,虽然” - 那又如何?使用'.php'文件。浏览器关心的是内容类型,而不是文件扩展名。 URI中没有文件扩展名。 – Quentin 2012-01-16 09:49:27

+0

对不起,我必须有.js文件。它被几十个其他网站上的脚本标签调用。 – Calvin 2012-01-16 09:51:49

+0

301永久移动 – Quentin 2012-01-16 09:52:46

你在哪里定义variableIWant

如果您只是将它分配给onreadystatechange函数,它只在该函数的范围内可用。

所以你必须要么外面声明它所有的功能或写

window.variableIWant = ajaxRequest.responseText; 

UPDATE:就像Quentin指出,只要把代码onreadystatechange函数内部...

或者:

ajaxRequest.onreadystatechange = function() { 
    if (ajaxRequest.readyState == 4) { 
     variableIWant = ajaxRequest.responseText; 
     longString = "The variable I retrieved is: "+variableIWant+". Isn't this nice?"; 
     document.getElementById('theDivPart').innerHTML = longString; 
    } 
} 

或:

ajaxRequest.onreadystatechange = function() { 
    if (ajaxRequest.readyState == 4) { 
     update(ajaxRequest.responseText); 
    } 
} 

function update(value) { 
    longString = "The variable I retrieved is: " + value + ". Isn't this nice?"; 
    document.getElementById('theDivPart').innerHTML = longString; 
} 

http://jsfiddle.net/roberkules/JgZ2B/

顺便说一下,有没有理由你不使用JavaScript框架?例如像jquery一样照顾所有的ajax麻烦?你在jQuery中的代码:

<script type="text/javascript"> 
$.get('http://sumary.org/phpfile.php').done(function(data){ 
    $(function() { 
     $('#theDivPart').html('The variable I retrieved is: ' + data + '. Isn\'t this nice?'); 
    }); 
}); 
</script> 

<body> 
<div id="theDivPart"></div> 
</body> 
+0

我有在所有函数之外定义的'variableIWant'。即使使用'window.variableIWant = ajaxRequest.responseText;'我仍然只是在字符串中使用undefined作为变量。是否有可能将其显示为未定义,但是当它更新时,它稍后会更改它? – Calvin 2012-01-16 19:19:33

+0

我认为这将是最好的,如果你会提供更多的代码...(也许上的jsfiddle)充分认识发生了什么事情。 – roberkules 2012-01-16 19:45:43

+0

http://jsfiddle.net/BqDST/ 这个例子将有警报显示正确的值(这是“正确”),而被放置在页面上的div标签字符串显示未定义的值 – Calvin 2012-01-16 19:55:50

所以为了简单起见,是有可能得到的variableIWant而在另一个字符串中使用它,还是我的运气,因为它必须等待readyState的?

它必须等待readystate函数被调用,并且直到HTTP响应返回才会发生。

把你的逻辑放到你为readystate分配的函数中,这就是readystate的用途。