必需的字符串参数'电子邮件'不在场

时间:2018-06-06 16:00:35

标签: angular spring-mvc

我正在为我的大学做一个简单的项目。我正在尝试发送电子邮件。

我在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;不存在)

1 个答案:

答案 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; }.

当您忘记了一个参数时,您的网址很好,但您的paramsbody而您没有options

修改

您的代码应为:

public sendEmail<T>(emailAddress: String) {
        return this.http.post<T>(this.getEmailUrl, {}, {params: {
            email: emailAddress
        }})
        .catch(error => {
            throw new Observable(error)
        });
}