Timeline for answer to JavaScript post request like a form submit by Rakesh Pai
Current License: CC BY-SA 4.0
Post Revisions
38 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 27, 2022 at 18:53 | comment | added | DGoiko | I used something like this in a Java Selenium-based crawler in order to start the crawling on a POST request (to avoid filling forms that did not charge properly). I executed a code-piece very similar to this one in a blank page generated on the fly. I had totally forgot, and remembered when I saw this response for another issue. Is there any modern simpler way to do this? PS: I love answers that do not introduce dependencies | |
| Mar 18, 2022 at 5:26 | comment | added | Wisarut Bholsithi | Just wonder how to assign the old input value into the new dynamically create <input>s in a form before submitting the form to the target URL | |
| S Mar 9, 2021 at 15:44 | history | edited | NullDev | CC BY-SA 4.0 |
added 2 characters in body
|
| Mar 7, 2021 at 5:41 | review | Suggested edits | |||
| S Mar 9, 2021 at 15:44 | |||||
| Oct 23, 2020 at 7:22 | comment | added | Lorenzo Von Matterhorn | So you are telling me there is no way in JavaScript to make a normal POST request without this HTML hack? | |
| Apr 22, 2020 at 15:21 | comment | added | Haddock-san | Clever solution. Instead of using JS to create the form, I manually created a form and added the hidden attribute to it with readonly in the text fields. Then I used JS to fill in the form and submit it. | |
| Apr 20, 2020 at 10:38 | comment | added | shireef khatab | @Rakesh and all, can someoneh help here please: stackoverflow.com/questions/61313006/… | |
| Nov 21, 2019 at 16:06 | comment | added | TRiG |
@www-0av-Com. The post here is not the HTTP verb, but the form method, which is case-insensitive. <form method="post"> is perfectly acceptable.
|
|
| May 3, 2019 at 20:47 | history | edited | andria_girl | CC BY-SA 4.0 |
Formatting and update to use modern syntax
|
| S Sep 3, 2018 at 19:32 | history | suggested | Community Bot | CC BY-SA 4.0 |
added jsdoc code, and warned that this will redirect the page.
|
| Sep 3, 2018 at 15:30 | review | Suggested edits | |||
| S Sep 3, 2018 at 19:32 | |||||
| Mar 13, 2018 at 16:27 | comment | added | Stefan Steiger | If value contains a dangeours xml character, this won't work in ASP.NET encodeUriComponent(value) is required. Then, UrlDecode is also required on the server-side. | |
| Jan 26, 2018 at 12:48 | comment | added | Damian Silkowski |
Hi, it works pretty well. But it always creates new element var form = document.createElement("form"); There is any possibilities to add it to already existing POST method?
|
|
| Jan 2, 2018 at 12:42 | comment | added | www-0av-Com | Shouldn't it be upper case "POST" (in the line method = method || "post")? I have never tried lower case, but I guess most IIS's don't care. I know of one IIS where lower case won't work. | |
| Nov 22, 2017 at 16:09 | comment | added | Davidson Lima | Python, Django and probably Flask users will see this error: "Forbidden (403). CSRF verification failed. Request aborted.", if creating a form from zero. In this case, you must pass the csrf token this way: post('/contact/', {name: 'Johnny Bravo', csrfmiddlewaretoken: $("#csrf_token").val()}); | |
| Oct 31, 2017 at 22:50 | comment | added | Andy | @stevemao depends if you need to support IE6-9 or not | |
| Oct 23, 2017 at 15:03 | history | edited | Philip Kirkbride | CC BY-SA 3.0 |
deleted 1 character in body
|
| Jul 10, 2017 at 9:01 | comment | added | Rakesh Pai | @Ethan It's an optional argument, and defaults to POST. Changing methods doesn't cost anything, so I just put it in there. Feel free to remove it if it bothers you. | |
| Jul 8, 2017 at 2:22 | comment | added | Ethan | @RakeshPai Why do you have the 'method' input arg when this is only for 'POST'? | |
| Jul 7, 2016 at 13:11 | comment | added | Ken Bellows | @Aerovistae The difference is that AJAX doesn't navigate to a new page, it just returns the data to the script that made the request. Hypothetically you could probably fake it by making an AJAX POST request with the content type set to "text/html" to get the content of the new page, then replace the current page with the HTML you get back, but the URL won't change, and doesn't the whole idea make you gag a little bit anyway? (That being said, similar methods are used for sections of pages in Single Page Application frameworks like Angular and Ember) | |
| Jul 7, 2016 at 13:07 | comment | added | Ken Bellows | @mricci The point of this snippet is to redirect the browser to a new URL specified by the action; if you're staying on the same page you can just use traditional AJAX to POST your data. Since the browser should be navigating to a new page, the current page's DOM's contents won't matter | |
| Jun 28, 2016 at 7:59 | comment | added | Deunz |
If you wanted this post to happen in a new tab : var tabWindowId = window.open('about:blank', '_blank'); var form = tabWindowId.document.createElement("form"); tabWindowId.document.body.appendChild(form);
|
|
| Jun 24, 2016 at 18:02 | comment | added | temporary_user_name | Is there no way to simulate this effect with ajax? What is the magic in the HTML form that accomplishes this, that it can't be done without it? | |
| May 31, 2016 at 17:19 | comment | added | Srneczek | why is method in parameters since its called post and should be used only for post? | |
| May 6, 2016 at 0:33 | comment | added | mricci |
Is it worth removing the form after it has been added to the DOM? I can foresee issues with document.forms
|
|
| Feb 17, 2016 at 8:02 | comment | added | eugene | amazing this is not natively supported either by html nor javascript not jquery .. you have to code this. | |
| Nov 21, 2015 at 9:19 | comment | added | lingceng | I created a gist with the code gist.github.com/lingceng/175f493450636e505cc3 | |
| Oct 20, 2015 at 16:41 | comment | added | emragins | Warning: Despite the many upvotes, this solution is limited and does not handle arrays or nested objects inside of a form. Otherwise it's a great answer. | |
| Oct 14, 2015 at 6:36 | comment | added | Scit | What about arrays in data params? Jquery post() interprets e.g.: "data: {array: [1, 2, 3]}" as ?array=1&array=2&array=3. Whis code gives another result. | |
| Feb 25, 2015 at 16:04 | history | edited | Liam | CC BY-SA 3.0 |
added 2 characters in body
|
| Sep 24, 2014 at 16:21 | review | Suggested edits | |||
| Sep 24, 2014 at 16:47 | |||||
| May 7, 2014 at 14:51 | history | edited | mikemaccana | CC BY-SA 3.0 |
Remove underscores, JS uses camelCase
|
| Feb 26, 2014 at 20:58 | history | edited | adamJLev | CC BY-SA 3.0 |
added example
|
| May 5, 2013 at 19:27 | history | edited | Yuck | CC BY-SA 3.0 |
deleted 1 characters in body
|
| Feb 11, 2012 at 14:18 | history | edited | Rakesh Pai | CC BY-SA 3.0 |
Added the hasOwnProperty check
|
| Jun 1, 2011 at 21:29 | history | edited | Rakesh Pai | CC BY-SA 3.0 |
deleted 45 characters in body
|
| Sep 25, 2008 at 16:24 | vote | accept | Joseph Holsten | ||
| Sep 25, 2008 at 15:28 | history | answered | Rakesh Pai | CC BY-SA 2.5 |