Best way to submit search criteria to server using AJAX?

Pay attention to the following HTML:

<input type='text' id='name'/>
<option id='option'>
  <select value='1'>hi</select>
  <select value='2'>bye</select>
</option>
<input type='text' id='date'/>

This is a very simple form used to obtain user search criteria. I need to send these search criteria to the server using an AJAX call. What is the best data type / structure that can be used for this? And why ?

I am currently thinking of a string and JSON object. So I'll either pass

"John|2|2010-10-10" 

to the server, or I'll pass

{"name":"John","option":2,"date":"2010-10-10"}

Perhaps there are other creative ways to address your search criteria? This criterion is removed to the server only once, it does not return to js.

Thank.

+3
source share
4 answers

HTML <form>, $.serializeArray() jQuery.

http://api.jquery.com/serializeArray/

$.serializeArray() JavaScript :

[ {name: a, value:1}, {name: b, value: 2}, ...]

, , . .

JSON ( serializeArray - ) . JSON . , , (|) . , , ||** . ( - !). JSON.

(, a = 1 & b = 2 c = 3), , $.serialize().

http://api.jquery.com/serialize/

+1

Google querystrings, :

search?name=John&option=2&date=2010-10-10

:

  • , - GET, POST, .
  • "API- ", .

:

  • URL-
  • ..

, , , JSON: , , , ( , ).

+1

It is always better to create your own data structure for data transfer. You can create the format 100% as you need, and expect it to be small, fast, and therefore effective.

The second best option is JSON, which is already quite lightweight and fast. But then again, not a single format will be your own.

Moreover, it is obvious, it is incompatible and not portable. Therefore, you must take this into account.

0
source

Source: https://habr.com/ru/post/1761936/


All Articles