user158625
user158625

Reputation: 261

Post request issue

I want to make a post request and i do like this:

        function Xxx_Click() {
            var params = new Array();
            var p1 = new Object();
            p1.Name = "id";
            p1.Value = 1; 
            params.push(p1);
            post('<%=Url.Action("Act","Ctrl")%>', params);
        }
        function post(url, params) {
            var form = document.createElement('form');
            form.action = url;
            form.method = 'POST';
            form.id = "fTest";
            for (var i = 0; i < params.length; i++) {
                var hidden = document.createElement('input');
                hidden.type = 'hidden';
                hidden.id = params[i].Name;
                hidden.name=params[i].Name;
                hidden.value = params[i].Value;
                form.appendChild(hidden);
            }
            $('#fTest').submit();
        }

but i don't reach the server side. I want to make a classic post and not to user $.post(...), how to make it right?

Upvotes: 0

Views: 75

Answers (2)

Pekka
Pekka

Reputation: 449415

I'm not sure what's causing the problem but fixing these points might do the trick:

Use the setAttribute method to set attributes.

form.setAttribute("action", url);
form.setAttribute("method", "POST");
...

Then, appendChild the form to the document. It's well possible a form has to be part of the document to be submitted within that document.

Upvotes: 1

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

Add this to the DOM before doing the actual submit. Actually the easier way will be have a form in the DOM and set the values instead of creating a new form.

Upvotes: 0

Related Questions