ZM191018

后端:

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);
                }
            }
        })
    });

 

测试:

分类:

技术点:

相关文章:

  • 2021-10-16
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
  • 2021-11-07
猜你喜欢
  • 2022-01-10
  • 2021-12-26
  • 2021-12-24
  • 2021-08-10
  • 2021-11-04
  • 2022-12-23
相关资源
相似解决方案