【问题标题】:Reactjs AXIOS Put requestReactjs AXIOS Put 请求
【发布时间】:2019-05-20 10:14:47
【问题描述】:

我有一个想要从后端更改的前端用户。所以我做了一个 AXIOS 查询,但它不起作用。

async updateUtilisateur(user) {
    const headers = {
        'authorization': localStorage.getItem(ACCESS_TOKEN)
    }

    const {id} = user.id;
    return await axios.put('/secure/utilisateur/${id}', 
        user,{headers}
    );
},

在我的后端日志中: o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/secure/utilisateur/${id}';反对'/gestion-promotions/secure/**'

2018-12-19 22:21:19.018 WARN 1432 --- [io-8080-exec-14] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:无法转换键入“java.lang.String”到所需类型“java.lang.Integer”;嵌套异常是 java.lang.NumberFormatException: For input string: "${id}"]

id 是我请求中的 PathVariable

【问题讨论】:

    标签: javascript axios


    【解决方案1】:

    您在此行中使用了错误的引号类型:

    return await axios.put('/secure/utilisateur/${id}',
    

    您正在使用 ''(单引号)而不是 ``(反引号) 这导致您的变量 id 未正确插值,然后您将字符串 ${id} 发送到后端。

    要解决这个问题,您可以使用反引号:

    return await axios.put(`/secure/utilisateur/${id}`,
    

    或:

    return await axios.put('/secure/utilisateur/' + id,
    

    更多关于 javascript 中反引号的信息:Template literals

    【讨论】:

    • 如何从前端直接向后端发送整数
    • 你做得对,如果你使用 put 你应该在 url 中发送 id,在使用它之前将字符串转换为 int 在后端:Integer.parseInt(id)
    • 显然不是,因为您告诉我将我的字符串转换为整数后端。这意味着我收到的是 Stnrig 而不是整数。我想直接收到。
    • 我的意思是,PUT 调用是正确的,但您不会收到Integer,因为您在将其发送为 URL 的一部分之前将其转换。 url 是一个长字符串,然后后端获取整个 url 字符串并提取 id,如果您想将 id 作为整数发送,您可以在调用正文中发送它,但这不是标准的做法它。
    • 如果我这样做,即通过将 id 放在请求上,它可以工作。 return await axios.put('/secure/utilisateur/1', user,{headers}
    猜你喜欢
    • 2022-12-21
    • 2023-03-12
    • 2021-10-05
    • 1970-01-01
    • 2020-01-12
    • 2019-01-29
    • 2018-05-20
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多