简体   繁体   English

从角度到弹簧的表格数据发布请求

[英]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(成功)我以这种方式发送数据:

  1. The format is form-data 格式是表单数据
  2. The key is reqData(mandatory) 关键是reqData(必填)
  3. The Value is json(mandatory) 价值是json(强制性)

后男子,请求

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.

相关问题 带有“表单数据”类型的请求正文参数的POST不起作用+角度2 - POST with request body params of type 'form-data' not working + angular 2 无法 POST 请求多部分/表单数据角度 - Cannot POST request multipart/form-data angular 多部分/表单数据发布请求在 Angular 7 和 spring 引导时出现 CORS 错误,仅当图像大小超过 1 mb 时 - multipart/form-data post request getting CORS error with Angular 7 and spring boot, only when image size is more than 1 mb 如何将多部分/表单数据从 Angular 发布到 Nodejs Multer? - How to post multipart/form-data from Angular to Nodejs Multer? passport.js 不适用于手动表单数据发布请求角度 - passport.js not working with manual form-data post request angular 在Angular2中构建multipart / form-data POST请求并验证输入类型File - Build multipart/form-data POST request in Angular2 and validate Input type File 如何从角度发布多部分/表单数据到Django REST API - How to post multipart/form-data from angular to Django REST API Angular 7 Post 文件内容作为 multipart/form-data - Angular 7 Post file contents as multipart/form-data 如何使用angular2中的form-data发送帖子 - How send post with form-data in angular2 Angular HttpClient发布两个文件(Multipart / form-data) - Angular HttpClient Post Two Files (Multipart/form-data)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM