使用从 angular 到 spring 的表单数据发布请求 [英] post request with form-data from angular to spring

查看:31
本文介绍了使用从 angular 到 spring 的表单数据发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 angular 及其 http 库向我的 spring rest api 发送一个 post 请求.

i am trying to send a post request to my spring rest api using angular and its http library.

目前在邮递员(成功)中,我正在以这种方式发送数据:

currently in post-man (sucessfully) i am sending the data in this way:

  1. 格式为表单数据
  2. 关键是 reqData(mandatory)
  3. 值为 json(强制)

如何通过angular以同样的方式发送数据?

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:

我的 rest api 看起来像这样:

my rest api looks like this:

@RequestMapping(value = "/login", method = RequestMethod.POST)

@ResponseBody
public ResponseEntity<String> handle(@RequestParam(value = "reqData") String reqData,HttpServletRequest request)

所以我应该发送一个键和值(我不知道打字稿中的哪个数据结构,但在java中它是MultiValueMap),其中键是reqData,值应该是字符串或json对象中的json.

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.

如何将我的 reqData json 转换为 MultiValueMap 格式?

我也尝试过 formData 和 Map:

i have tried both formData and Map also:

const formData: FormData = new FormData();
    formData.append('reqData', JSON.stringify(reqData));

const map = new Map();
    map.set('reqData', reqData);

推荐答案

这是我向后端发送请求所做的工作.

This is what i have done to send request to my backend.

我创建了所需的json,然后我创建了一个请求参数字符串

i created the required json,then i created a request parameter string

const reqData = 'reqData=' + jsonDataInString;

发送成功请求.

这篇关于使用从 angular 到 spring 的表单数据发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆