gpt4 book ai didi

javascript - 来自本地服务器上项目的 servlet 的 Angular Http.get 请求(json 数据正确返回,但不显示,可以与模拟 api 一起使用)

转载 作者:行者123 更新时间:2023-12-03 04:13:06 25 4
gpt4 key购买 nike

控制台错误:

Error with getting users from service. Response with status: 404 Not Found for URL: http://localhost:8080/ChatApp/GetAllUsersServlet

当我在某处部署外部项目时,会出现同样的问题,所以使用 url: http://java.cyclone2.khleuven.be:38034/ChatApp/GetAllUsersServlet你可以自己看到一个包含 json 的有效 url,但仍然出现 404 错误。

Angular 代码需要来自本地服务器上运行的 servlet 的 json:

export class UserService {
// private usersUrl = 'api/users'; // mock api
private usersUrl = 'http://localhost:8080/ChatApp/GetAllUsersServlet'; // external users from local server
private headers = new Headers({'Content-Type': 'application/json'});

constructor(private http: Http) { }
getUsers(): Promise<User[]> {
return this.http.get(this.usersUrl)
.toPromise() // Http.get returns RxJS Observeable, converted to Promise here
.then(response => response.json().data as User[]) // .data for mock inMemoryDataService
.catch(this.handleError);
}

Servlet 返回什么:

[{"fname":"TestFname","password":"test","gender":"Female","name":"TestName","id":1,"email":"test@test.com","age":21,"username":"Test","status":"offline"},{"fname":"Test4Fname","password":"test","gender":"Female","name":"Test4Name","id":4,"email":"test4@test.com","age":21,"username":"Test4","status":"offline"},{"fname":"Test3Fname","password":"test","gender":"Female","name":"Test3Name","id":3,"email":"test3@test.com","age":28,"username":"Test3","status":"offline"},{"fname":"Test2Fname","password":"test","gender":"Male","name":"Test2Name","id":2,"email":"test2@test.com","age":22,"username":"Test2","status":"offline"}]

模拟 api 中的这件事确实给出了正确的结果:

import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
createDb() {
let users = [{"fname":"TestFname","password":"test","gender":"Female",
"name":"TestName","id":1,"email":"test@test.com","age":21,"username":"Test","status":"offline"},
{"fname":"Test4Fname","password":"test","gender":"Female",
"name":"Test4Name","id":4,"email":"test4@test.com","age":21,"username":"Test4","status":"offline"},{"fname":"Test3Fname","password":"test","gender":"Female","name":"Test3Name","id":3,"email":"test3@test.com","age":28,"username":"Test3","status":"offline"},
{"fname":"Test2Fname","password":"test","gender":"Male",
"name":"Test2Name","id":2,"email":"test2@test.com","age":22,"username":"Test2","status":"offline"}]
return {users};
}
}

任何帮助将不胜感激,因为我真的不知道为什么它不起作用。尝试了类似的东西,但只是更少的 json 数据,并且有效。是的,servlet 的服务器正在本地运行。

getUsers() 被使用并显示,但由于它适用于模拟数据,所以这应该没问题?:

export class UsersComponent  {
users: User[];
selectedUser: User;

constructor(
private userService: UserService,
private router: Router) { }

gotoInfo(): void {
this.router.navigate(['/info', this.selectedUser.username]);
}
onSelect(user: User): void {
this.selectedUser = user;
}
getUsers(): void {
this.userService.getUsers().then(users => this.users = users);
}
ngOnInit(): void {
this.getUsers();
}
}

Servlet(启用cors):

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserDB db = new UserDB();

JSONArray array = new JSONArray();
for (User users: db.getAll()) {
try {
array.put(users.getJSONObject());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// setting the response type to json
response.setContentType("application/json");
// setting the CORS request
response.setHeader("Access-Control-Allow-Origin", "*");

response.getWriter().write(array.toString());

最佳答案

以前使用 in-memory-web-api 会扰乱您的 http 请求,除非您删除类似以下内容 InMemoryWebApiModule.forRoot(InMemoryDataService)来自您的 NgModule,那么您的请求应该会顺利进行。

编辑:根据代码注释实现,您知道以下内容:

完成此操作后,我还可以指出您的获取请求中存在问题,因为就目前情况而言,您将无法在组件中获取任何数据。您的响应仅包含一个数组,而不是 data ,所以它应该是:

.then(response => response.json() as User[]) 

<罢工>

关于javascript - 来自本地服务器上项目的 servlet 的 Angular Http.get 请求(json 数据正确返回,但不显示,可以与模拟 api 一起使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44246151/

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