无法让我的JS代码正常工作

时间:2013-07-28 16:43:20

标签: javascript ajax

我的JS和HTML是一个JS小提琴,here。我把JSON对象放在CSS的位置,因为我没有任何CSS,所以我不认为它会导致问题。问题是我无法让它工作......我认为我的ajaxCall函数中的某个地方有错误,但我找不到它。错误消息显示

  

GET .... [HTTP / 1.1 304未修改   12毫秒]

我在本地网络服务器上尝试使用Wamp。

JSON文件:

{

"adrBook": [

  {
  "name": "Nikola Markovic",
  "email": "nikola@gmail.com",
  "location": "Serbia"
  },
  {
  "name": "Petar Nikolic",
  "email": "petar@gmail.com",
  "location": "Germany"
  }
  {
  "name": "Marko Stojanovic",
  "email": "markos@gmail.com",
  "location": "Swedish"
  },
  {
  "name": "Dusan Uzelac",
  "email": "dusanu@gmail.com",
  "location": "Holand"
  },
  {
  "name": "Petar Grujic",
  "email": "petarg@gmail.com",
  "location": "USA"
  },
  {
  "name": "Nikola Jesic",
  "email": "nikolaj@gmail.com",
  "location": "China"
  },
  {
  "name": "John Man",
  "email": "johnm@gmail.com",
  "location": "Japan"
  },
  {
  "name": "Jeniffer Gray",
  "email": "jenifferg@gmail.com",
  "location": "Bosnia"
  },
  {
  "name": "Marc Brown",
  "email": "marcb@gmail.com",
  "location": "Croatia"
  },
  {
  "name": "Nikola Ilic",
  "email": "nikolai@gmail.com",
  "location": "Macedonia"
  },
  {
  "name": "Nemanja Nikolic",
  "email": "nemanjan@gmail.com",
  "location": "Slovenia"
  },
  {
  "name": "Stefan Nikolic",
  "email": "stefann@gmail.com",
  "location": "Romania"
  }
  ]
  }

1 个答案:

答案 0 :(得分:0)

当点击<button>内的<form>时,问题在于浏览器的默认行为。默认情况下,这会导致提交表单,从而导致页面重新加载。因此,停止执行您的JavaScript代码,并且XMLHttpRequest永远不会完成(或者,甚至可能已经开始)。

您可以通过调用事件对象的preventDefault()方法阻止浏览器重新加载页面:

search: function(event) {
    event.preventDefault();
    // everything else stays the same …

通过此更改,您的其余代码将会运行,您将面临评论中指出的JSON语法错误。快乐的调试! :)