Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery POST does not submit form values using IE 11

I have tried to find a solution for this problem for hours now, but the following code is simply not working for Internet Explorer 11. It does work for Chrome and Firefox. When using IE11 the post is submitted but the submitted form is empty.

<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
            <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
        </head>
        <body>
            <form action="/Mandate/Edit" id="mandateForm" method="post">            
                <input id="ExternalId" name="ExternalId" type="hidden" value="" />
                <input id="mandateName" name="mandateName" type="text" />
                <a href="#" id="md-submit">Create</a>
            </form>
            <script type="text/javascript">
                $(function () {
                    $("#md-submit").on("click", function (e) {
                        e.preventDefault();
                        var form = $("#mandateForm");
                        var request = $.ajax({
                            type: "POST",
                            url: form.attr("action"),
                            data: {
                                mandateName: "test4711"
                            },
                            dataType: 'json',
                            cache: false
                        });
                    });
                });
            </script>
        </body>
    </html>

Thank you very much for your help.

like image 612
user2567190 Avatar asked May 26 '14 08:05

user2567190


1 Answers

The serialize() method does not convert form data to Json...

This should work--the ouput in IE11 will be {"ExternalId":"","mandateName":"4343"}:

<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
            <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
        </head>
        <body>
            <form action="/Mandate/Edit" id="mandateForm" method="post">            
                <input id="ExternalId" name="ExternalId" type="hidden" value="" />
                <input id="mandateName" name="mandateName" type="text" />
                <a href="#" id="md-submit">Create</a>
            </form>
            <script type="text/javascript">
                function form_to_json (selector) {
                  var ary = selector.serializeArray();
                  var obj = {};
                  for (var a = 0; a < ary.length; a++) obj[ary[a].name] = ary[a].value;
                  return JSON.stringify(obj);
                }

                $(function () {
                    $("#md-submit").on("click", function (e) {
                        e.preventDefault();
                        var form = $("#mandateForm");
                        var request = $.ajax({
                            type: "POST",
                            url: form.attr("action"),
                            data: form_to_json(form),
                            dataType: 'json',
                            cache: false
                        });
                    });
                });
            </script>
        </body>
    </html>
like image 138
bastos.sergio Avatar answered Sep 22 '22 02:09

bastos.sergio