Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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); 

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

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?

EDIT: Basically, what I want is to write:

$.goTo(url, params); 

And optionally

$.goTo(url, params, "POST"); 
like image 277
itsadok Avatar asked Jul 19 '09 08:07

itsadok


1 Answers

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); 
like image 88
Dustin Avatar answered Sep 30 '22 14:09

Dustin