我有一个Uncaught ReferenceError:当我试图从控制器类获取服务时,响应未定义异常。所以我有一个像这样的控制器:
package com.finartz.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.finartz.beans.UsersBean;
@Controller
public class UserController {
@RequestMapping(value="/userList",method={RequestMethod.GET})
@ResponseBody //for to get answer in JSON format !
public List< UsersBean > listUsers(){
List < UsersBean > users = new ArrayList<UsersBean>();
UsersBean user = new UsersBean();
user.setName("Süleyman");
user.setPhone("0545 542 17 00");
users.add(user);
UsersBean user2 = new UsersBean();
user2.setName("Zeynep");
user2.setPhone("0545 500 17 00");
users.add(user2);
return users;
}
}
而且我有一个像这样的javascript文件:
$(document).ready(function(){
$("#showUsersButton").click(function(){
showUsers();
});
});
function showUsers(){
$.ajax({
url: "/finartzOdev/userList",
context: document.body
}).done(function() {
var table = "";
//we say class='table' to bootstrap !
table += "<table class='table'>";
table += "<thead>";
table += "<tr>";
table += "<th>" + "Name" + "</th>";
table += "<th>" + "Phone" + "</th>";
table += "</tr>";
table += "</thead>";
// I get the exception here !.
response.forEach(function(users){
table += "<tr>";
table += "<td>" + user.name + "</td>";
table += "<td>" + user.phone + "</td>";
});
table += "<table>";
$(".userListDiv").html(table)
});
}
当我试图从中获取用户信息时
服务在这一行:response.forEach(function(users)我得到异常。
最佳答案
您必须在Ajax response
函数中接收done()
才能使用它
}).done(function( response) {
var table = "";
//we say class='table' to bootstrap !
table += "<table class='table'>";
table += "<thead>";
table += "<tr>";
table += "<th>" + "Name" + "</th>";
table += "<th>" + "Phone" + "</th>";
table += "</tr>";
table += "</thead>";
// I get the exception here !.
response.forEach(function(users){
table += "<tr>";
table += "<td>" + user.name + "</td>";
table += "<td>" + user.phone + "</td>";
});
table += "<table>";
$(".userListDiv").html(table)
});