jQuery 的getjson的回调函数不运行

test.html

<!DOCTYPE html>




click me $(document).ready(function(){ $("button").click(function(){ alert("before getjson"); $.getJSON("test.json",function(data){alert("did it!");}); alert("after getjson"); }); });


test.json

{ "people": [

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

]}

本地模式运行会存在同源问题,需要将test.html 和 test.json 放在同级目录下,放在静态文件服务器或者应用服务器上,以服务器模式运行才可以正常
访问到。

语句的符号和括号要检查一下,格式化后比较容易看。