[英]post request with form-data from angular to spring
i am trying to send a post request to my spring rest api using angular and its http library. 我正在尝试使用angular和它的http库向我的spring rest api发送一个帖子请求。
currently in post-man (sucessfully) i am sending the data in this way: 目前在post-man(成功)我以这种方式发送数据:
how to send the data in the same way via angular? 如何通过角度以相同的方式发送数据?
currently, this is how my data looks like: 目前,这是我的数据的样子:
onSignIn(form: NgForm) {
const email = form.value.email;
const password = form.value.password;
const reqData = {
'app_uname': email,
'app_pass': password
};
}
adding more about my backend code: 添加更多关于我的后端代码:
my rest api looks like this: 我的休息api看起来像这样:
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> handle(@RequestParam(value = "reqData") String reqData,HttpServletRequest request)
so i should be sending a key and value ( i am not aware which data structure in typescript,but in java it is MultiValueMap) where the key is reqData and the value should be json in string or json object. 所以我应该发送一个键和值(我不知道打字稿中的哪个数据结构,但在java中它是MultiValueMap),其中键是reqData,值应该是字符串或json对象中的json。
how to make my reqData json in angular to MultiValueMap format? 如何使我的reqData json在角度为MultiValueMap格式?
i have tried both formData and Map also: 我也尝试过formData和Map:
const formData: FormData = new FormData();
formData.append('reqData', JSON.stringify(reqData));
const map = new Map();
map.set('reqData', reqData);
To send data to server using formdata using can do this like this 使用formdata使用发送数据到服务器可以这样做
const email = form.value.email;
const password = form.value.password;
const formData: FormData = new FormData();
formData.append("email ", email);
formData.append("password ", password);
// then http post to server
Edit 编辑
if you want to submit the data as json you need to do like this 如果你想以json的形式提交数据,你需要这样做
const email = form.value.email;
const password = form.value.password;
const reqData = {
'app_uname': email,
'app_pass': password
};
const formData: FormData = new FormData();
formData.append("reqData", JSON.stringify(reqData));
// then http post to server and in the server you need to parse the json string
Hope it solve your problem. 希望它能解决你的问题。 Please let me know if this solve your problem
如果这可以解决您的问题,请告诉我
you can create a method to post as below: 您可以创建一个发布方法,如下所示:
var model = {email: youremail, password: yourpassword};
post(url: string, model: any): Observable <any> {
let formData: FormData = new FormData();
formData.append('app_uname', model.email);
formData.append('app_pass', model.password);
return this._http.post(url, formData)
.map((response: Response) => {
return response;
}).catch(this.handleError);
}
This is what i have done to send request to my backend. 这就是我向后端发送请求所做的工作。
i created the required json,then i created a request parameter string 我创建了所需的json,然后我创建了一个请求参数字符串
const reqData = 'reqData=' + jsonDataInString;
which sent a success request. 发送了成功请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.