雁过请留痕...
代码改变世界

Jquery调用ajax参数说明

2017-12-22 11:22  xiashengwang  阅读(428)  评论(0编辑  收藏  举报

代码中有详细注释,直接上代码。

注释掉的选项,一般用不到,直接用最基本的部分就可以了。

            $.ajax({
                // 请求的URL
                url: '../Daily/Daily_Report',

                //HTTP method:"POST", "GET", "PUT"
                // 这样写也行 method: 'POST',
                type: "POST",

                // 发送到服务器的数据
                data: {
                    "user":"abc"
                },

                // default: true
                // 是否异步,默认是异步的
                //async: true,
                
                // default: true
                // 发送数据前,会将data属性指定的object转换成query string
                // 指定成false后,object的数据在后台得不到
                //processData: true,

                // default: 'application/x-www-form-urlencoded; charset=UTF-8'
                // values: application/x-www-form-urlencoded, multipart/form-data, or text/plain
                // 发送给服务器的数据类型,UTF-8编码是固定的,改不了的。
                // 设定为false,就是不设定contentType给服务器,可以省略
                //contentType: false,

                // 服务器返回的数据类型,不指定的话,jquery回根据服务器指定的MIME进行推断。
                // 所以,如果服务器指定了MINE类型,可以省略
                //dataType:"json",

                // 发送前调用的函数
                // 如果返回false,将取消ajax请求
                //beforeSend: function (xhr, settings) {
                //},

                // 对返回数据的预处理
                // 这里的data是原始的response的数据,比success函数的data要早,
                // 如有需要,可以对返回的原始值进行处理,然后再返回data
                //dataFilter:function(data, type){
                //},

                // 成功时调用的函数
                // 这里的data已经是根据dataType,格式化好的数据,比如一个json对象。
                success: function (data, textStatus, xhr) {
                    if (data !== null) {
                    }
                },

                // 异常时调用的函数
                //  Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror".
                // When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
                error: function (xhr, textStatus, errorThrown) {
                },

                // ajax请求完成调用的函数,这个函数在success和error后调用,不论成功或失败都要做的操作放在这里
                //textStatus:"success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror"
                complete: function(xhr, textStatus){
                }
            });