Javascript 使用jQuery的基本Ajax get请求

Javascript 使用jQuery的基本Ajax get请求,javascript,jquery,spring-mvc,Javascript,Jquery,Spring Mvc,在我的Spring MVC 3.2项目中,我对非常基本的get请求有问题 以下是我的ajax请求: $(document).ready(function(){ function getApartmentList(){ console.log("get"); $.ajax({ type: 'GET', url: '/getApartmentList', dataType: "json",

在我的Spring MVC 3.2项目中,我对非常基本的get请求有问题

以下是我的ajax请求:

$(document).ready(function(){

  function getApartmentList(){
      console.log("get");
      $.ajax({
          type: 'GET',
          url: '/getApartmentList',
          dataType: "json",
          contentType: "application/json",
          success: function(data){
              console.log("U are cool !!!");
             console.log(JSON.stringify(data));
          },
          error: function(xhr){
              console.log("error: " + xhr.status );
          }
      });
    }

  $("#testBtn").on("click", function(){
      getApartmentList();
  });


});
以下是服务器方法:

@RequestMapping(value="/getApartmentList", method = RequestMethod.GET)
@ResponseBody
public List<Apartment> getList(){

    Apartment ap = new Apartment();
    ap.setAdress("Norra Stationsgatan");
    ap.setHouseNumber(99);
    ap.setZip(11364);

    Apartment ap2 = new Apartment();
    ap2.setAdress("Birkagatan");
    ap2.setHouseNumber(32);
    ap2.setZip(11339);

    List<Apartment> list = new ArrayList<Apartment>();
    list.add(ap);
    list.add(ap2);

    return list;
}
因此,首先是我没有找到的方法,然后是它被发现,我做错了什么。 您可以在以下位置浏览整个项目:

您没有返回json,因此请删除
数据类型:“json”


您好,当您访问时,您会得到什么?请检查url是否正常工作。一旦尝试在Browseri中打开url(),该方法是否在控制器中?所以,也许控制器也有一个映射。如果是这样,您的url将类似于:localhost:8080//GetApartmentListThank!!!!!曼努埃尔·里查兹(Manuel Richarz)加入了项目主路径,我也开始工作。在预览项目上使用Tomcat8,我不需要项目路径。似乎玻璃鱼是不同的。
GET http://localhost:8080/getApartmentList 404 (Not Found) jquery-2.1.0.js:8556
XHR finished loading: GET "http://localhost:8080/getApartmentList".  
$(document).ready(function(){
      function getApartmentList(){
          console.log("get");
          $.ajax({
              type: 'GET',
              contentType: "application/json",
              url: '/getApartmentList',
              success: function(data){
                  console.log("U are cool !!!");
                 console.log(JSON.parse(data));
              },
              error: function(xhr){
                  console.log("error: " + xhr.status );
              }
          });
        }

  $("#testBtn").on("click", function(){
      getApartmentList();
  });


});