我正在为我的大学做一个简单的项目。我正在尝试发送电子邮件。
我在SPRING MVC中的控制器
@RequestMapping(value = "/send-mail", method = RequestMethod.POST)
public ResponseEntity<String> sendEmailToClient(@RequestParam(name="email") final String email) {
//some code
}
}
我的角度查询
public sendEmail<T>(emailAddress: String) {
return this.http.post<T>(this.getEmailUrl, {params: {
email: emailAddress
}})
.catch(error => {
throw new Observable(error)
})
}
但我收到了一个错误。
POST http://localhost:8080/M.S.-Handloom-Fabrics/mail/send-mail 400(必填字符串参数&#39;电子邮件&#39;不存在)
答案 0 :(得分:0)
我认为您在http.post(...)
中犯了一个错误。你忘记了一个参数。
根据 Angular docs about HttpClient post ,这是post
方法的定义:
post(url: string, body: any | null, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
}): Observable<ArrayBuffer>
所以你应该有3个参数:
url
:Type: string
。body
:Type: any | null
。options
:Type: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: 'body'; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType: 'arraybuffer'; withCredentials?: boolean; }.
当您忘记了一个参数时,您的网址很好,但您的params
是body
而您没有options
。
您的代码应为:
public sendEmail<T>(emailAddress: String) {
return this.http.post<T>(this.getEmailUrl, {}, {params: {
email: emailAddress
}})
.catch(error => {
throw new Observable(error)
});
}