jquery $.post() vs $.get()

前端 未结 5 1081
[愿得一人]
[愿得一人] 2021-02-04 14:47

I need to retrieve a simple page and use the data that it returns. Is there any difference between $.post() and $.get() should I be using one over the

相关标签:
5条回答
  • 2021-02-04 15:06

    The main difference between them is that with POST you pass a collection of data and with GET you pass data in the URL. If you're passing a lot of data I'd suggest POST. If you're just calling a URL for a response then use get.

    For a full understanding though checkout the jQuery documentation of each.

    GET: http://api.jquery.com/jQuery.get/

    POST: http://api.jquery.com/jQuery.post/

    0 讨论(0)
  • 2021-02-04 15:10

    If you just want to retrieve the contents from an html document, use $.load() instead.

    You can even retrieve partial information from that document by providing an additional selector:

    $('#result').load('ajax/test.html');
    $('#result').load('ajax/test.html #justThisContainerPlease');
    

    see http://api.jquery.com/load/


    To answer your question more generally, its no big difference whether you're using a POST or a GET request to a server, it depends on the amount of data you need to send. Typically, a GET request is limited to 2083 (because IE limits the query-string). So if you have a lot of data to send, you should use a POST request.

    Technically, a GET request should be slightly faster. Because internally only one packet is sent instead of at least two (one for the header and one for the transmission body). But that really is high performance optimizing.

    0 讨论(0)
  • 2021-02-04 15:29

    Here is a nice article explaining the differences between an HTTP POST and an HTTP GET. I myself prefer to use $.ajax(); and tweak it accordingly.

    0 讨论(0)
  • 2021-02-04 15:30

    If you're not submitting data, then you actually should be using $.load();

    $.get(); and $.post() are typically for submitting data to a server, so you don't need them in this context. There are big differences between POST and GET data, you should take some time to read up on them.

    0 讨论(0)
  • 2021-02-04 15:30

    Go for $.get() as you don't need to post any data, or $.load() if you want to display the page in the browser (you want to refresh a part of the page).

    0 讨论(0)
提交回复
热议问题