Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用httpClient的post方法在url中传递参数_Angular_Http_Post_Parameters_Angular7 - Fatal编程技术网

Angular 使用httpClient的post方法在url中传递参数

Angular 使用httpClient的post方法在url中传递参数,angular,http,post,parameters,angular7,Angular,Http,Post,Parameters,Angular7,我需要在url中发送一封电子邮件作为http参数。我用postman做了同样的尝试,代码很好,但是下面的angular代码没有将email参数添加到url中。我使用angular 7作为前端 http://localhost:8090/api/auth/forgot?email=abcd@gmail.com 电子邮件发送到abcd@gmail.com当使用邮递员和上述网址 以下是前端代码 import { Injectable } from '@angular/core'; import {

我需要在url中发送一封电子邮件作为http参数。我用postman做了同样的尝试,代码很好,但是下面的angular代码没有将email参数添加到url中。我使用angular 7作为前端

http://localhost:8090/api/auth/forgot?email=abcd@gmail.com
电子邮件发送到abcd@gmail.com当使用邮递员和上述网址

以下是前端代码

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { PasswordInfo } from './password-info';

@Injectable({
  providedIn: 'root'
})
export class ResetPswdEmailService {

  constructor(private http: HttpClient) { }

  sendemail( emailinfo : String){//emailinfo is the email address
    let data = {email: emailinfo};
    return this.http.post<any>('http://localhost:8090/api/auth/forgot', {params:data});
  }


}
控制台中的错误

http://localhost:8090/api/auth/forgot 400


HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "OK", url: "http://localhost:8090/api/auth/forgot", ok: false, …}
error: {timestamp: "2019-05-07T10:53:58.230+0000", status: 400, error: "Bad Request", message: "Required String parameter 'email' is not present", path: "/api/auth/forgot"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://localhost:8090/api/auth/forgot: 400 OK"
name: "HttpErrorResponse"
ok: false
status: 400
statusText: "OK"
url: "http://localhost:8090/api/auth/forgot"
似乎尚未将参数添加到url

我在http的正文中发送了这封电子邮件,它工作得很好。所以这里的问题是设置参数


提前感谢您。

您可以尝试以下方法:

let params = new HttpParams();
params = Params.append('email', emailInfo);

return this.http.post<any>(`http://localhost:8090/api/auth/forgot`, { params: params });
let params=new-HttpParams();
params=params.append('email',emailInfo);
返回this.http.post(`http://localhost:8090/api/auth/forgot`,{params:params});

您可以尝试以下方法:

let params = new HttpParams();
params = Params.append('email', emailInfo);

return this.http.post<any>(`http://localhost:8090/api/auth/forgot`, { params: params });
let params=new-HttpParams();
params=params.append('email',emailInfo);
返回this.http.post(`http://localhost:8090/api/auth/forgot`,{params:params});

如果您的请求已发布。就这样改变吧:

return this.http.post<any>(`http://localhost:8090/api/auth/forgot`, {email: emailinfo});
返回this.http.post(`http://localhost:8090/api/auth/forgot`,{电子邮件:emailinfo});

如果您的请求已发布。就这样改变吧:

return this.http.post<any>(`http://localhost:8090/api/auth/forgot`, {email: emailinfo});
返回this.http.post(`http://localhost:8090/api/auth/forgot`,{电子邮件:emailinfo});

我认为您希望将电子邮件作为查询参数发送,而不是作为请求正文发送,因此您必须执行以下操作:

sendemail( emailinfo : String){//emailinfo is the email address
   let data = {email: emailinfo};
   return this.http.post<any>('http://localhost:8090/api/auth/forgot', null, {params:data});
}
sendmail(emailinfo:String){//emailinfo是电子邮件地址
let data={email:emailinfo};
返回此.http.post('http://localhost:8090/api/auth/forgot,null,{params:data});
}

否则,如果这是您想要的,您需要知道使用POST方法不是正确的方法,因为POST数据意味着使用正文,所以您使用的是GET方法。

我认为您希望将电子邮件作为查询参数发送,而不是作为请求正文发送,因此您必须执行以下操作:

sendemail( emailinfo : String){//emailinfo is the email address
   let data = {email: emailinfo};
   return this.http.post<any>('http://localhost:8090/api/auth/forgot', null, {params:data});
}
sendmail(emailinfo:String){//emailinfo是电子邮件地址
let data={email:emailinfo};
返回此.http.post('http://localhost:8090/api/auth/forgot,null,{params:data});
}

否则,如果这是您想要的,您需要知道这不是使用POST方法的正确方式,因为POST数据意味着使用主体,所以您将其用作GET方法。

尝试使用concatating,如
返回此。http.POST('http://localhost:8090/api/auth/forgot/?email="(电邮),触发此api。转到浏览器检查并选择网络,查看api hitI用错误更新问题后此链接的外观。似乎参数没有添加到urltry concating中,比如
返回this.http.post('http://localhost:8090/api/auth/forgot/?email="(电邮),触发此api。转到浏览器检查并选择网络,查看api hitI用错误更新问题后此链接的外观。似乎未将参数添加到URL中。您应该指出,
HttpParams
不会对字符进行编码,如
:,“…
出于某种原因。我偶然发现这个问题,必须以urlencoded格式传递数据。它不起作用。我在控制台日志中打印了参数,并且输入的值存在。但是电子邮件还没有发送到我的邮箱。后端没有任何错误,因为使用PostMany时代码工作正常。您应该提到,
HttpParams
不会对字符进行编码,如
:,“…
出于某种原因。我偶然发现这个问题,必须以urlencoded格式传递数据。它不起作用。我在控制台日志中打印了参数,并且输入的值存在。但是电子邮件还没有发送到我的邮箱。后端没有任何错误,因为代码使用PostMandand可以正常工作。不要忘记调用组件代码上的subscribe。我更新了代码的组件部分,出现了上面问题中控制台中的错误。不要忘记调用组件代码上的subscribe。我更新了代码的组件部分,出现了上面问题中控制台中的错误上述问题