JSON结构在IE7中不起作用(JavaScript) [英] JSON structure not working in IE7 (JavaScript)

查看:125
本文介绍了JSON结构在IE7中不起作用(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我是JSON的新手,所以如果我的问题有点a昧,请原谅我-我的头撞墙了太多,需要专家的建议.

OK, I'm new to JSON so please forgive me if my question is a little ignorant - I've beat my head against the wall too much and need some expert advice.

我试图得到一个简单的示例,其中HTML页面使用jQuery的JSON/AJAX函数来调用示例PHP页面,该示例PHP页面传回一个简单的JSON数据结构,然后该页面使用jQuery编写一个从数据结构到页面的元素.在FF中工作.在IE7 arrrggghhhh中不起作用!

I'm trying to get a simple example working where an HTML page uses jQuery's JSON/AJAX functions to make a call to an example PHP page which passes back a simple JSON data structure and then the page uses jQuery to write one of the elements from the data structure to the page. Works in FF. Doesn't work in IE7 arrrggghhhh!

HTML代码(相关部分):

HTML Code (relevant parts):

<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript">
function testAJAX() {
  $.getJSON("test-ajax.php", function(json){
      $("#div1").html(json.var1[1]);
  });
}
</script>
</head>
<body>
<input type="button" value="test ajax" onclick="testAJAX();" />
<div id="div1"> </div>
</body>

PHP代码(test-ajax.php):

PHP Code (test-ajax.php):

<?php
include_once('./json.php'); 
$output = array('var1' => array('value1a', 'value1b', 'value1c'), 
                'var2' => array('value2a', 'value2b', 'value2c')); 

header("Content-type: text/plain");
echo json_encode($output);
?>

json.php具有我正在使用的json_encode()函数-我是从 http://us2.php.net/manual/en/function.json-encode.php .我之所以使用它,是因为我没有PHP5,而且我的系统管理员不会为它安装任何支持.只需在浏览器中直接查看test-ajax.php,就会打印出一个序列化的JSON结构,如下所示:

json.php has the json_encode() function I'm using - I got it from http://us2.php.net/manual/en/function.json-encode.php. I'm using it because I don't have PHP5 and my sys admins won't install any support for it. Just viewing test-ajax.php directly in your browser prints a serialized JSON structure like this:

{"var1":["value1a","value1b","value1c"],"var2":["value2a","value2b","value2c"]}

,它似乎是有效的JSON结构.在我的JavaScript中,我尝试通过执行以下操作引用'value1b':json.var1[1]. FF处理得很好. IE7表示未定义.

which appears to be a valid JSON structure. In my JavaScript I'm trying to reference 'value1b' by doing this: json.var1[1]. FF handles this just fine. IE7 says that is undefined.

我做错了什么?我该如何转移像这样的二维数组

What am I doing wrong? How do I take transfer a 2-d array like this

array('var1' => array('value1a', 'value1b', 'value1c'), 
      'var2' => array('value2a', 'value2b', 'value2c'));

通过JSON/AJAX吗?还是不可能?

over JSON/AJAX? Or is this not possible?

推荐答案

确定了.根本不是JSON/JavaScript问题,而是缓存问题.当我进行此开发时,我必须在test-ajax.php无法正常工作或正在生成其他JSON结构时最初测试IE7,然后将test-ajax.php更改为上面发布的内容,然后更新了JavaScript BUT IE7,使用最初从test-ajax.php接收到的内容的缓存版本.我对此进行了测试-如果我清除IE7中的缓存,则它可以工作,然后如果我更改JSON结构中的值(但不更改结构本身),则IE7将继续使用缓存的JSON结构.

OK, figured it out. Wasn't a JSON/JavaScript issue at all but a caching issue. When I was developing this I must have initially tested IE7 when test-ajax.php wasn't working or was producing a different JSON structure and then I changed test-ajax.php to what I posted above and I updated my JavaScript BUT IE7 was using a cached version of what it originally received from test-ajax.php. And I tested this - if I clear the cache in IE7 it works and then if I change the values in the JSON structure (but not the structure itself) IE7 continues to use the cached JSON structure.

解决方案:

我添加了

header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0"); 

在我的test-ajax.php页面中,现在IE7在调用AJAX时正在正确地检查服务器是否有较新版本的test-ajax.php.

in my test-ajax.php page and now IE7 is checking the server properly for a newer version of test-ajax.php when it makes an AJAX call.

谢谢大家!

这篇关于JSON结构在IE7中不起作用(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆