0

I need to create a POST request with $.ajax that would be the equivalent of submitting the following form:

    <form method="POST" action="test">
      <input name="foo" value="1">
      <input name="bar" value="2">
      <input name="foo" value="3">
      <input name="bar" value="4">
    </form>

In particular, I have values with same names, which need to be passed in the correct (possibly interleaving) order, so in the case of a GET request foo=1&bar=2&foo=3&bar=3 would be correct and foo=1&foo=3&bar=2&bar=3 would be incorrect.

I looked into the traditional setting, but I haven't managed to construct the data parameter correctly.

Currently, I am using jquery_form for such use cases, but I did not find any way to cancel the requests send by this and it still needs a form, which I sometimes create dynamically and remove it later again.

Is there a better way that creating the fully encoded body of the request manually? Maybe there is even a solution for requests that need multipart/form-data encoding without having to do the encoding manually?

Viktor Dick
  • 111
  • 2
  • 1
    I think I found a way in https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax, using `FormData`. – Viktor Dick Jun 24 '23 at 08:01
  • yes, `FormData` is your answer. so you could remove your question – Alexander Nenashev Jun 24 '23 at 09:32
  • As a side note to the issue, having your business logic require the request parameters to be in a specific order is a ***huge*** code smell. I'd ask why you feel you need this, and suggest you approach the problem in a different way so that this requirement can be removed. – Rory McCrossan Jun 24 '23 at 14:58
  • That's a convention in the `Zope` framework. If a submitted name has the form `x.y:records` and occurs multiple times, it is collected into a list of dictionaries named `x` with key `y` on the server side. Whenever a name is encoundered that is already present in the current row, a new row is started. So it nicely matches the transfer of a table of input fields. – Viktor Dick Jun 25 '23 at 10:36
  • Regarding deleting the question: Even though there is another question which gave me the answer, it took quite a while to find it starting with the *ordering* requirement. I thought by answering my own question and linking to the original answer I could save someone else this time, but somehow klicking "Answer your Question" only creates a comment and not an actual answer... – Viktor Dick Jun 25 '23 at 10:39

0 Answers0