账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    怎么用json把后端的数据传到前端html界面显示,javascript要怎么写?
    61
    0

    //后端代码

    public JsonResult SendData()
        {
            return Json( new {username="刘伟",teacherid="2432012522" });
        }
        

    //前端代码

    <div class="box5" style="position:relative;">
        <!-- 背景图的div -->
        <img style="position:absolute;margin-left: 5%;margin-top: 9%" src="~/Content/themes/portrait.png" width=28% height=50% />
        <img src="~/Content/themes/background.jpg" width=100% height=100% />
        <div class="FONT4" style="position:absolute; z-index:2; top:15px">姓名:刘伟</div>
        //怎么把这个“刘伟”替换成动态的,由后端发送的?
        <div class="FONT4" style="position:absolute; z-index:2; top:100px">教工号:2432012522</div>
        //怎么把这个“2432012522”替换成动态的,由后端发送的?
    </div>
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 一笑脸就大。 普通会员 1楼

      在前端页面中,我们可以使用JavaScript的JSON对象将后端数据解析为JSON格式,并将其显示在HTML页面上。以下是一个简单的例子:

      首先,后端数据可以存储在一个JSON文件中,例如:

      json { "name": "John Doe", "age": 30, "city": "New York" }

      然后,在前端页面中,我们可以使用JavaScript的JSON对象将这个数据解析为一个JavaScript对象:

      javascript fetch('/api/data') .then(response => response.json()) .then(data => { console.log(data.name); // 输出 "John Doe" console.log(data.age); // 输出 "30" console.log(data.city); // 输出 "New York" });

      这段代码首先使用fetch函数从后端获取数据,然后使用JSON.parse函数将数据解析为JavaScript对象。最后,我们可以在控制台中打印出解析后的数据。

      注意,这只是一个基本的例子。在实际应用中,你可能需要处理更复杂的数据结构,或者使用更复杂的解析方法。此外,你还需要确保后端服务器能够正确地处理你的请求,并返回JSON格式的数据。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部