Non-ajax GET/POST using jQuery (plugin?)

前端 未结 7 1690
南旧
南旧 2020-12-04 10:11

This is one of those situations where I feel like I\'m missing a crucial keyword to find the answer on Google...

I have a bag of parameters and I want to make the br

7条回答
  •  一生所求
    2020-12-04 10:56

    This is similar to the above except it will handle Array parameters being passed to MVC

    (function($) {
        $.extend({
            getGo: function(url, params, traditional) {
                /// 
                ///     Perform a GET at url with specified params passed as part of the query string.
                /// 
                /// 
                /// 
                /// Boolean:  Specify true for traditional serialization.
                document.location = url + '?' + $.param(params, traditional);
            },
            postGo: function (url, params) {
                /// 
                ///     Perform a POST at url with the specified params as part of a form object.
                ///     If a parameter is an array then it will submit it as multiple attributes so MVC will see it as an array
                /// 
                /// 
                /// 
                var $form = $("
    ").attr("method", "post").attr("action", url); $.each(params, function (name, value) { if (value.length != null && value.length > 0) { for (var i = 0; i < value.length; i++) { $("").attr("name", name + '[' + i + ']').attr("value", value[i]).appendTo($form); } } else { $("").attr("name", name).attr("value", value).appendTo($form); } }); $form.appendTo("body"); $form.submit(); } }); })(jQuery);

提交回复
热议问题