首页 > 解决方案 > Angular,当 url 中提到变量时,变量不会被其值替换。例如:${用户名}

问题描述

我在springboot中使用@pathvariable从客户端接收数据。变量是用户名、密码和电子邮件。

我检查了变量的值是否传递给了服务。是的,当我在控制台日志中打印它时,它就通过了。

当变量在 url 中分配为 /${username)/${password)//${email) 时,不是获取这些变量的值,而是 ${username) 存储在数据库中,而不是输入的用户名在用户界面中。

通过 Postman 验证,url 中指定的值用于在数据库中创建记录,工作正常。

附上 Rest Controller 代码和服务代码:

Rest Controller 
_________________

@RestController
public class UserResource {

@Autowired
private UserService userService;

@RequestMapping(value = "/user/{username}/{password}/{email}",
        method = RequestMethod.POST)

public ResponseEntity  newUserPost(@PathVariable("username") String username,
@PathVariable("password") String password,
@PathVariable("email") String email,
HttpServletResponse response)
throws Exception {

registerUser function in Service
________________________________

    registerUser(username: string, password: string, email: string) {
    let params = 'username='+username+'&password='+password;
 console.log(params);

    let url = 'http://localhost:8080/user/${username}/${password}/${email}'
    let headers = new HttpHeaders({
'Content-Type' : 'application/x-www-form-urlencoded',
        });
      return this.http.post(url, {headers: headers, responseType : 'text'});
}

变量的值需要发送到弹簧靴。

标签: angularspring-boot

解决方案


if 应该包裹在 `` 不是单个 qoutes 周围

let url = `http://localhost:8080/user/${username}/${password}/${email}`

推荐阅读