在jQuery中使用getJSON函数时,javascript – 回调函数不起作用

我试图在jQuery中使用getJSON函数来导入一些数据并触发一个回调函数。回调函数不运行。但是,如果我尝试与get函数相同的东西,它的工作正常。奇怪的是,即使我通过“json”作为类型,它也可以使用get函数。为什么会发生这种情况?我在Firefox 3和IE 7中测试了以下文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<Meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<title>ajax test</title>
<script type="text/javascript" src="/jquery-1.3.2.min.js"></script>
</head>
<body>
<input type="button" id="test1" value="get">
<input type="button" id="test2" value="getJSON">
<input type="button" id="test3" value="get with json type">
<script type="text/javascript">
$("#test1").click(function() {
    $.get("index.html",function(response) {
            alert('hi');
            //works
        }
    )
});

$("#test2").click(function() {
    $.getJSON("index.html",function(response) {
            alert('hi');
            //doesn't work
        }
    )
});

$("#test3").click(function() {
    $.get("index.html",function(response) {
            alert('hi');
            //works
        },"json"
    )
});
</script>
</body></html>

无论我访问什么URL,只要它在同一个域上,这似乎都会发生。我尝试传递一些数据,这并没有什么意义。

当然,我可以通过使用get函数解决这个问题,就像我在第三个测试函数中一样,但是我仍然很好奇为什么会发生这种情况。

我知道这里有一个similar question,但没有回答我的问题。

解决方法

json需要有效,否则回调将不会触发。

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...