我有一个表单,包含多个无线电输入和一个文本区域输入,我使用axios从ReactJs客户端发送.请求如下所示:

  axios.post("/wellbeing/" + params.wellbeingSurveyType, { formAnswersJson: formAnswers })
    .then(res => {
      // logic
    })
    .catch(err => {
      // logic
    })

The 'formAnswers' object looks like this: enter image description here

然后,我从Spring控制器接收到如下请求:

    @PostMapping("{wellbeingSurveyType}")
    public WellbeingSurveySubmission submitSurvey(
            @PathVariable WellbeingSurveyType wellbeingSurveyType,
            @RequestBody String formAnswersJson) throws JsonProcessingException {
        var result = new ObjectMapper().readValue(formAnswersJson, HashMap.class);
        return new WellbeingSurveySubmission(); //ignore this
    }

When I call the 'toString()' method on the result object it seems to correctly print out the map values: enter image description here

But when I try to actually operate on the object (which is parsed as a LinkedHashMap) I cannot access the keys or values: enter image description here

When I try to open up the object using the debugging tool it seems to store a reference to itself as a value: enter image description here

我想要的结果就是一张 map <弦,弦>这代表了JSON,但我不确定为什么会发生这种行为.

如果您能提供任何帮助或建议,我们将不胜感激,谢谢.

推荐答案

好吧,我发现实现这一点的最佳方法是在axios post请求中解构JSON对象,如下所示:

axios.post("/wellbeing/" + params.wellbeingSurveyType, { ...formAnswers })
        .then(res => {
          // logic
        })
        .catch(err => {
          // logic
        })

如果我只是传递formAnswers对象,效果会更好.它不必要地包装对象,即包含单个键值对"formAnswers"的hashmap.

尽管正如冻结对象所提到的,最好定义一个专用的表单对象,并将其作为spring控制器中的参数.

Reactjs相关问答推荐

在react中向数组状态添加值时出错

Reaction Native:在初始获取后,Reducer变为未定义

Next.js-图像组件加载问题

react 路由GUGUD Use Effect无法通过useNavigate正常工作

有没有办法在Shopify中显示自定义计算的交货日期?

折叠并重新打开react 手风琴将重置内部状态

无法使用导入app/page.jsx的组件中的tailwind 类文本操作和自定义动画

Chart.js如何go 除边框

.populate() 方法在 mongodb 中不起作用

生产部署:控制台中 Firebase 无效 api 密钥错误

Next.js `getLayout` 函数没有被调用

ReactJS 动态禁用按钮

需要在窗口调整大小时更新 React 组件

react 事件顺序

调用 Statsig.updateUser 时 UI 不呈现

React Router 6.4CreateBrowserRouter子元素不显示

如何在react 日历中自定义带有圆形边框的日期项?

当我在其中传递 2 个箭头函数时,为什么我的 onClick 事件不适用于 useState

react-admin useGetIdentity 只返回全名,id 未定义 头像未定义

将 dict 值转换并插入到react 钩子的列表中