0

what is the difference between type:'get' and type:'post'

  $.ajax({
                type: 'GET', /'POST'
                url: '../common/AjaxPage.aspx',
                data: { action: 'SaveSafeData', safecost: cost, safeTime: Duration },
                cache: false
            });
user973063
  • 31
  • 2
  • 8

3 Answers3

2

One sends an HTTP GET request, while the other sends an HTTP POST.

Basically, the difference is with GET, the data is sent in the url (like thing.php?action=doStuff&value=20), while with POST, the data is sent in a separate header.

Semantically, if you're fetching data from the server and not changing anything, you should generally use GET, while if you're changing something you should use POST.

Peter C
  • 6,219
  • 1
  • 25
  • 37
1

The 'type' option refers to the HTTP method types(GET/POST/PUT/DELETE/..) for the Ajax request. Default is GET; Except GET&POST others are not supported by all browsers. There are corresponding shorthand functions in jQuery Ajax for this method types: jQuery.get() uses Http Get, jQuery.post() uses Http Post.

If you need to know more about the GET vs POST, here are some SO Q&As that are very helpful:

When do you use POST and when do you use GET?

https://stackoverflow.com/questions/340704/get-vs-post-in-html-forms

Is either GET or POST more secure than the other?

Community
  • 1
  • 1
Kibria
  • 1,865
  • 1
  • 15
  • 17
0

The type of GET or POST is from how request in hypertext transfer protocol (HTTP) works. So you should really google http POST or http GET to learn what are their differences. Note, check out also other request methods in HTTP.

But in the context of jquery, here is the snippet of the entire documentation. Note, check out also what is the currently HTTP request methods supported by jquery ajax().

Sending Data to the Server

By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option. This option affects how the contents of the data option are sent to the server. POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.

The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.

Community
  • 1
  • 1
Jasonw
  • 5,054
  • 7
  • 43
  • 48