gpt4 book ai didi

javascript - 我有一个未捕获的 ReferenceError : response is not defined exception when I am trying to get service

转载 作者:行者123 更新时间:2023-11-30 15:26:40 25 4
gpt4 key购买 nike

当我试图从 Controller 类获取服务时,我有一个 Uncaught ReferenceError: response is not defined 异常。所以我有一个这样的 Controller :

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 done() 函数中接收到 response 才能使用它

}).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)


});

关于javascript - 我有一个未捕获的 ReferenceError : response is not defined exception when I am trying to get service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42854132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com