首页 > 解决方案 > Reactjs AXIOS Put 请求

问题描述

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

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

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

在我的后端日志中:osswumatcher.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

标签: javascriptaxios

解决方案


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

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

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

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

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

或者:

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

有关 javascript 中反引号的更多信息:模板文字


推荐阅读