$.ajax()与$.post()区别

Posted on 2018-08-10 09:43  郑小胖  阅读(470)  评论(0编辑  收藏  举报

当使用$.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对象的格式。