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

前端 未结 7 1688
南旧
南旧 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 11:04

    Here's what I ended up doing, using the tip from redsquare:

    (function($) {
        $.extend({
            doGet: function(url, params) {
                document.location = url + '?' + $.param(params);
            },
            doPost: function(url, params) {
                var $form = $("<form method='POST'>").attr("action", url);
                $.each(params, function(name, value) {
                    $("<input type='hidden'>")
                        .attr("name", name)
                        .attr("value", value)
                        .appendTo($form);
                });
                $form.appendTo("body");
                $form.submit();
            }
        });
    })(jQuery);
    

    Usage:

    $.doPost("/mail/send.php", {
        subject: "test email",
        body: "This is a test email sent with $.doPost"
    });
    

    Any feedback would be welcome.

    Update: see dustin's answer for a version that works in IE8

    0 讨论(0)
提交回复
热议问题