【发布时间】:2017-12-04 12:28:55
【问题描述】:
我正在尝试使用创建的 Rest 微服务来实现 HTTP 服务 GET 和 POST。
我能够从我的 Rest 微服务中获取数据。
但我无法发出 POST 请求。
下面是我的 Rest 微服务代码
WebController.java
package com.central.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.central.model.Users;
import com.central.repository.UsersRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@RestController
public class WebController {
private static final String NULL = null;
@Autowired
UsersRepository userRepo;
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping("/checkUsers")
public String checkLogin() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List<Users> useObj = (List<Users>) userRepo.findAll();
return (mapper.writeValueAsString(useObj));
}
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/checkByCredential", method = RequestMethod.POST)
public String checkLoginByName(@RequestBody Users user) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Users useObj1 = userRepo.findByUsernameAndPassword(user.username, user.password);
return (mapper.writeValueAsString(useObj1));
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { BasicService } from './basic.service';
@NgModule({
declarations : [ AppComponent],
imports : [BrowserModule,HttpModule,FormsModule ],
providers : [BasicService],
bootstrap : [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import { BasicService } from './basic.service';
@Component({
selector: 'app-root',
template: `<h1>{{ title }}</h1>
<div style="margin-left:50px; height: 200px; overflow: auto;">
<table>
<tr><td>Username</td>  <td>Password</td></tr>
<tr *ngFor= " let item of data">
<td>{{ item.username }}</td>  <td>{{ item.password }}</td>
</tr>
</table>
</div>
<h2>Login</h2>
<form role="form">
<div ng-control-group="credentials">
<label for="username">Username</label>
<input
type="text"
#username
id="username"
ng-control="username"
required>
<label for="password">Password</label>
<input
type="password"
#password
id="password"
ng-control="password"
required>
</div>
<button (click)="checkByCredential(username,password)">Login</button>
</form>`
})
export class AppComponent {
title: string;
data: any;
constructor(private MyService: BasicService) {
this.title = "Angular Service";
this.MyService.GetUsers()
.subscribe(users => { this.data = users });
}
checkByCredential(username: string, password: string) {
this.MyService.checkByCredential(username, password).subscribe(users => { this.data = users });
}
}
basic.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class BasicService {
constructor(private http: Http) { }
GetUsers() {
return this.http.get('http://localhost:8080/checkUsers')
.map(result => result.json());
}
checkByCredential(username: string, password: string) {
const user = { username: username, password: password };
return this.http
.post('http://localhost:8080/checkByCredential', user)
.map(result => result.json());
}
}
当我通过表单提交数据时,我在控制台中收到此错误,如下图所示
但是当我尝试使用用户名和密码发出 Post 请求时,使用我的邮递员,我能够取回如下图所示的数据
任何机构都可以帮我解决 Angular 2 中的 Post 请求
【问题讨论】:
-
你能在邮递员中检查请求参数,并将它与你从 Angular 应用程序发出的请求进行比较
-
确实,考虑到Spring返回的是400(错误请求),请求参数肯定有差异。还要检查 Spring 启动应用程序的日志以及浏览器中调用的完整响应,这通常可以很好地指出问题所在。
-
非常感谢您的回复..好的,我会尝试这样做
-
此错误是由您的框架生成的...您应该首先检查 chrome 上的网络选项卡并检查您的请求正文以确保它与您期望的相符。根据您服务中的代码,我想它会的。接下来,您在控制台中看到,您的服务器实际上正在给您一条错误消息,您应该展开它以便我们可以看到它是什么。
-
好的,我会编辑我的问题
标签: angular spring-boot