js中发送请求到后端接口,报404错误,求大神帮助

js代码:

//获取用户数据
document.addEventListener("DOMContentLoaded", function () {
    var context=document.getElementById("spanId").textContent;
    var colonIndex=context.indexOf(":");
    username=context.substring(colonIndex+1).trim();
    // 发送Ajax请求获取用户数据
    fetch("/User/info?username=" + username)
    .then(response => {
        console.log(response.status);
        console.log(response.json()) ;
            if (!response.ok) {
                throw new Error('服务器返回错误.');
            }
        // 检查响应的内容类型是否为 JSON
        const contentType = response.headers.get('content-type');
        if (contentType && contentType.includes('application/json')) {
            return response.json();
        } else {
            throw new Error('服务器返回的不是有效的 JSON 数据');
        }
    }).then(data => {
            // 将获取的数据填充到页面中
            console.log("服务器响应数据",data);
            function fillUserData(data) {
                // 填充用户数据到页面中
                document.querySelector(".input-field[name='name']").value = data.name;
                document.querySelector(".input-field[name='age']").value = data.age;
                document.querySelector(".input-field[name='gender']").value = data.gender;
                document.querySelector(".input-field[name='birthdate']").value = data.birthdate;
                document.querySelector(".input-field[name='address']").value = data.address;
                document.querySelector(".input-field[name='hobbies']").value = data.hobbies;
                document.querySelector(".input-field[name='education']").value = data.education;
                document.querySelector(".input-field[name='occupation']").value = data.occupation;
                document.querySelector(".input-field[name='phone']").value = data.phone;
                document.querySelector(".input-field[name='email']").value = data.email;
            }
            fillUserData(data);
        }).catch(error => {
            // 处理异常情况
            console.error('异常:', error);
        });

springmvc代码:

package com.controller;
import com.dao.Users;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/User")
public class UsersController{
    @Autowired
    private UsersService userService;
    @GetMapping("/info")
    public Users getUserByUsername(@RequestParam String username) {
        return userService.getUserByUsername(username);
    }
}

服务器URL:http://localhost:8003/MT

错误:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值