0

How to perform post action by redirecting to the page using jquery.I tried something like this

    $.ajax({
              type: "POST",
              url: response.data.myUrl,
              data: JSON.stringify(response.data.myParam),
              dataType: "json"
         });

I need to redirect to the url posting data to it.But this isnt working out.Can anyone please help me.

1 Answers1

0

You can serialize form fields into JSON and stringify the result.

        /**
         * Serialize form fields into JSON
         **/
        (function($){
        $.fn.serializeJSON = function(){
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                     if (o[this.name] !== undefined) {
                            if (!o[this.name].push) {
                                o[this.name] = [o[this.name]];
                            }
                            o[this.name].push(this.value || '');
                        } else {
                            o[this.name] = this.value || '';
                        } 
            });
            return o;
        }
        })(jQuery);

So you'll have one string with all fields, put it into an hidden field and submit the form:

myString= JSON.stringify($('#myForm').serializeJSON());
$("#myForm #myString").val(myString);
$("#myForm").attr("action", YOUR_URL);
$("#myForm").submit();

This is supposed to be the form:

<form id="myForm" method="POST">
    <input type="hidden" id="myString" />
</form>
dauricchio
  • 56
  • 7