我的控制器中有一个Web服务,它接受三个字符串参数。它工作正常(使用Postman测试)。我有一个表格,它将从angular2.Here' s发送thees参数我的控制器: `@RestController 公共类RegisterAPI {
@Autowired
RegisterMetier registerMetier;
@RequestMapping(value="/register",method = RequestMethod.POST)
public void register(@RequestParam("userName") String userName,@RequestParam("password") String password,@RequestParam("email") String email){
registerMetier.save(userName, email, password);
}`
现在我的typeScript代码(服务):
@Injectable()
export class AddThreshold {
constructor (
private http: Http
) {}
add(userName:any,password:any,email:any) {
const body=JSON.stringify({ userName:userName, password:password,email:email });
console.log('9raaahom');
const headers = new Headers({ 'Content-Type': 'application/json'});
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers,withCredentials : true });
return this.http.post(
http://localhost:8080/register ,body,options)
.map((data:Response) => data.status);
}
对于我的组件:
`@Component({
moduleId: module.id,
selector: 'app-signup',
templateUrl: './signup.component.html',
providers: [AddThreshold],
styleUrls: ['./signup.component.scss']
}) 导出类SignupComponent实现OnInit {
angular = false;
x={};
y={};
constructor(
private AddClass: AddThreshold
) {}
ngOnInit()
{
}
submit (userName:any,password:any,email:any)
{
console.log(userName);console.log(password);console.log(email);
this.AddClass.add(userName,password,email)
.subscribe(data => console.log("here"+data));
console.log("end add ")
;
}
} `
和我的webPageComponent: `
<div class="form-group">
<input type="text" class="form-control input-underline input-lg" id="" placeholder="Email" #email >
</div>
<div class="form-group">
<input type="password" class="form-control input-underline input-lg" id="" placeholder="Password" #password>
</div>
<div class="form-group">
<input type="password" class="form-control input-underline input-lg" id="" placeholder="Repeat Password">
</div>
</div>
<a class="btn rounded-btn" (click)="submit(userName.value,email.value,password.value)" > Register </a>
<a class="btn rounded-btn" [routerLink]="['/login']"> Log in </a>
</form>
</div>
</div>
`