使用jQuery的非ajax GET / POST(插件?) [英] Non-ajax GET/POST using jQuery (plugin?)

查看:126
本文介绍了使用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");


推荐答案

jQuery插件看起来效果很好,直到我尝试了IE8。我必须做一些小修改才能让它在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(插件?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆