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

Non-ajax GET/POST using jQuery (plugin?)(使用 jQuery(插件?)的非 ajax GET/POST)
本文介绍了使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)