简体   繁体   English

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

[英]jquery $.post() vs $.get()

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 other? $.post()$.get()之间是否有任何区别?

I don't plan on submitting any data with the request. 我不打算随请求提交任何数据。

If you just want to retrieve the contents from an html document, use $.load() instead. 如果您只想从html文档中检索内容,请改用$.load()

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/ 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. 要更一般地回答您的问题,无论您是对服务器使用POST还是GET请求都没有太大区别,这取决于您需要发送的数据量。 Typically, a GET request is limited to 2083 (because IE limits the query-string). 通常, GET请求限制为2083(因为IE限制查询字符串)。 So if you have a lot of data to send, you should use a POST request. 因此,如果要发送大量数据,则应使用POST请求。

Technically, a GET request should be slightly faster. 从技术上讲, GET请求应该稍快一些。 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. 但这确实是高性能优化。

Here is a nice article explaining the differences between an HTTP POST and an HTTP GET. 这是一篇很好的文章,解释了HTTP POST和HTTP GET之间的区别。 I myself prefer to use $.ajax(); 我自己更喜欢使用$.ajax(); and tweak it accordingly. 并据此调整它。

If you're not submitting data, then you actually should be using $.load(); 如果您没有提交数据,那么您实际应该使用$.load();

$.get(); and $.post() are typically for submitting data to a server, so you don't need them in this context. $.post()通常用于向服务器提交数据,因此在此上下文中不需要它们。 There are big differences between POST and GET data, you should take some time to read up on them . POST和GET数据之间存在很大差异,您应该花一些时间来阅读它们

如果你想在浏览器中显示页面(你想刷新页面的一部分$.get() ,可以使用$.get()因为你不需要发布任何数据,或$.load() )。

The main difference between them is that with POST you pass a collection of data and with GET you pass data in the URL. 它们之间的主要区别在于,使用POST传递数据集合,使用GET传递URL中的数据。 If you're passing a lot of data I'd suggest POST. 如果您传递了大量数据,我建议POST。 If you're just calling a URL for a response then use get. 如果您只是为响应调用URL,请使用get。

For a full understanding though checkout the jQuery documentation of each. 通过签出每个的jQuery文档来全面了解。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM