使用 jQuery(插件?)的非 ajax GET/POST

时间:2023-03-27
本文介绍了使用 jQuery(插件?)的非 ajax GET/POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我觉得我错过了在 Google 上找到答案的关键关键字的情况之一......

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

我有一袋参数,我想让浏览器导航到带有参数的 GET URL.作为一个 jQuery 用户,我知道如果我想发出 ajax 请求,我会这样做:

I have a bag of parameters and I want to make the browser navigate to a GET URL with the parameters. Being a jQuery user, I know that if I wanted to make an ajax request, I would simply do:

$.getJSON(url, params, fn_handle_result);

但有时我不想使用 ajax.我只是想提交参数并返回一个页面.

But sometimes I don't want to use ajax. I just want to submit the parameters and get a page back.

现在,我知道我可以循环参数并手动构造一个 GET URL.对于 POST,我可以动态创建一个表单,用字段填充它并提交.但我确信有人已经编写了一个可以做到这一点的插件.或者也许我错过了一些东西,你可以用核心 jQuery 来做.

Now, I know I can loop the parameters and manually construct a GET URL. For POST, I can dynamically create a form, populate it with fields and submit. But I'm sure somebody has written a plugin that does this already. Or maybe I missed something and you can do it with core jQuery.

那么,有人知道这样的插件吗?

So, does anybody know of such a plugin?

基本上,我想要写的是:

Basically, what I want is to write:

$.goTo(url, params);

可选

$.goTo(url, params, "POST");

推荐答案

在我在 IE8 上试用之前,jQuery 插件似乎运行良好.我必须做些小改动才能让它在 IE 上运行:

jQuery Plugin seemed to work great until I tried it on IE8. I had to make this slight modification to get it to work on IE:

(function($) {
    $.extend({
        getGo: function(url, params) {
            document.location = url + '?' + $.param(params);
        },
        postGo: function(url, params) {
            var $form = $("<form>")
                .attr("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);

这篇关于使用 jQuery(插件?)的非 ajax GET/POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Jquery:在您键入时过滤下拉列表 下一篇:Jquery Chosen 插件 - 通过 Ajax 动态填充列表

相关文章

最新文章