0

是否可以扩展 jQuery$.post()辅助函数的属性和选项。

我有时需要添加更多的属性$.post(),喜欢async, beforeSend, contentType, context, crossDomain, error, global, headers, ifModified, mimeType, timeout, etc但我避免使用$.ajax(),因为我太习惯使用$.post(),因为我的习惯。


编辑:是否可以覆盖/覆盖整个辅助插件/功能?

4

1 回答 1

1

这是添加您自己的参数的函数,它也会更新 get 方法。但是你可以清楚地看到它只是包装 $.ajax

(直接取自 jQuery 源代码)

jQuery.each( [ "get", "post" ], function( i, method ) {
    jQuery[ method ] = function( url, data, callback, type ) {
        // shift arguments if data argument was omitted
        if ( jQuery.isFunction( data ) ) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajax({
            type: method,
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
});

编辑 - 示例修改代码所有参数都是必需的

jQuery.each( [ "myPost" ], function( i, method ) {
    jQuery[ method ] = function( url, data, callback, type, errorCallback ) {
        return jQuery.ajax({
            type: method,
            url: url,
            data: data,
            success: callback,
            dataType: type,
            error: errorCallback
        });
    };
});
于 2013-03-01T12:34:54.713 回答