我正在尝试从jQuery移动网站的javascript文件中检索信息。默认情况下启用Ajax,但是当我尝试xmlHttpRequest.send()时,responseText是页面的源代码而不是json结构。 initialize()函数在pageinit上运行,所以我的想法是它正在检索的json应该在调用时存在。此外,initialize()在网站的非移动变体上运行良好,所以我认为它与JQM处理ajax请求的方式有关。提前感谢您的任何帮助。
<!DOCTYPE html>
<html>
<head>
var xmlHttpRequest;
var json;
<script type="text/javascript">
function initialize()
{
xmlHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() :
new ActiveXObject("Msxml2.XMLHTTP");
if (xmlHttpRequest == null)
return;
xmlHttpRequest.open("GET", "pick.js", false);
xmlHttpRequest.send();
json = eval('('+ xmlHttpRequest.responseText +')');
}
</script>
......
</head>
<body>
<div data-role="page" id="map-page">
<script type="text/javascript">
$('#map-page').live('pageinit',function(){
initialize();
});
</script>
.....
</div>
</body>
</html>
答案 0 :(得分:0)
由于您正在使用jQuery Mobile(因此,jQuery),您应该考虑使用jQuery.ajax - 它会处理所有“硬件”,比如为您创建XHR对象。
根据您的情况,您的代码如下所示:
function initialize() {
$.get("pick.js", function(data, status, jqXHR) {
//when the call succeeds, do something with the 'data' param
console.log(data);
}, "script");
}