zoukankan      html  css  js  c++  java
  • ajax传递对象参数,后台使用FromBody接收对象参数

    后端:

    1、Modal:创建一个模型参数对象usermsg

    public class usermsg
        {
            public string username { get; set; }
    
            public string password { get; set; }
        }

    2、Controllers:将该对象类型作为参数类型

    前端:

    1、HTML:添加一个按钮button3

    2、javascript事件:为button3添加一个事件

    与传递字符串相比有两个不同点:

      其一:ajax对象参数多了一个属性contentType,值为application/json

      contentType:'application/json'

      其二:data参数中需要将对象序列化为json字符串

      data:JSON.stringify({username: 'zhangsan', password: '123456789'})
    $('#btnGetUser').click(function(){
            $.ajax({
                url:'http://localhost:53179/values/api/getuser',
                dataType:'json',
                type:'Post',
                contentType:'application/json',
                data:JSON.stringify({username: 'zhangsan', password: '123456789'}),
                success:function(res){
                    console.log(res)
                    if(res!=null){
                        $('#name').prop('value',res.username);
                        $('#age').prop('value',res.age);
                    }
                }
            })
        });

    测试:

  • 相关阅读:
    单独设置css的class属性
    理解闭包的使用方法
    npm常用命令和总结
    前端调试之服务器
    gulp 报错的处理——个人经验
    工作经验备忘
    c++:虚函数和纯虚函数(转载)
    snmp学习、配置
    sigar学习
    linux安装VSCode
  • 原文地址:https://www.cnblogs.com/ZM191018/p/13859844.html
Copyright © 2011-2022 走看看