$.ajax与$.post、$.get的一点区别

后台代码:

复制代码
[HttpPost]
        public string DoLogin(string username,string password)
        {
            return "success";
        }
复制代码

当使用$.ajax时:

复制代码
var name = $('#txtUserName').val();
                var pwd = $('#txtPassWord').val();
                var param = "{username:'" + name + "',password:'" + pwd + "'}";
                $.ajax({
                    type: "POST",
                    url: "/Main/DoLogin",
                    data: param,
                    contentType: "application/json;charset=utf-8",
                    success: function (msg)
                    {
                        var test = msg;
                        var test2 = test;
                    },
                    error: function (e)
                    {
                        var test = e;
                        var test2 = test;
                    }
                });
复制代码

当使用$.post时:

复制代码
var name = $('#txtUserName').val();
                var pwd = $('#txtPassWord').val();
                var param = { username: name, password: pwd };
                $.post('/Main/DoLogin', param, function (result) {
                    var test = result;
                    alert(test);
                });
                
复制代码

区别在于参数的写法,使用$.ajax时参数要写成JSON字符串的格式,使用$.post是参数要写成JSON对象的格式

转载于:https://www.cnblogs.com/asdyzh/p/9807264.html

你可能感兴趣的:($.ajax与$.post、$.get的一点区别)