問題描述
如何確定哪種方法最適合某種情況?誰能提供一些示例來了解功能和性能方面的差異?
How can I find out which method is best for a situation? Can anybody provide some examples to know the difference in terms of functionality and performance?
推薦答案
XMLHttpRequest
是 jQuery 封裝成更實用、更簡化的形式和跨瀏覽器一致功能的原始瀏覽器對象.XMLHttpRequest
is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality.jQuery.ajax
是 jQuery 中的通用 Ajax 請求器,可以進行任何類型和內容的請求.jQuery.ajax
is a general Ajax requester in jQuery that can do any type and content requests.jQuery.get
和jQuery.post
只能發出 GET 和POST 請求.如果你不知道這些是什么,你應該檢查 HTTP 協議 并了解一點.這兩個函數在內部使用jQuery.ajax
,但它們使用您不必自己設置的特定設置,因此與使用jQuery.ajax
相比,簡化了 GET 或 POST 請求.GET 和 POST 是最常用的 HTTP 方法(與 DELETE、PUT、HEAD 甚至其他很少使用的外來方法相比).jQuery.get
andjQuery.post
on the other hand can only issue GET and POST requests. If you don't know what these are, you should check HTTP protocol and learn a little. Internally these two functions usejQuery.ajax
but they use particular settings that you don't have to set yourself thus simplifying GET or POST request compared to usingjQuery.ajax
. GET and POST being the most used HTTP methods anyway (compared to DELETE, PUT, HEAD or even other seldom used exotics).所有 jQuery 函數都在后臺使用
XMLHttpRequest
對象,但提供了您不必自己做的額外功能.All jQuery functions use
XMLHttpRequest
object in the background, but provide additional functionality that you don't have to do yourself.因此,如果您使用 jQuery,我強烈建議您僅使用 jQuery 功能.完全忘記
XMLHttpRequest
.使用合適的 jQuery 請求函數變體,在所有其他情況下使用$.ajax()
.所以別忘了還有其他常用的jQuery Ajax相關函數到$.get()
、$.post()
和$.ajax()
.好吧,您可以對所有請求使用$.ajax()
,但是您必須編寫更多代碼,因為它需要更多選項來調用它.So if you're using jQuery I strongly recommend that you use jQuery functionality only. Forget about
XMLHttpRequest
altogether. Use suitable jQuery request function variations and in all other cases use$.ajax()
. So don't forget there are other common jQuery Ajax related functions to$.get()
,$.post()
and$.ajax()
. Well you can just use$.ajax()
for all of your request, but you will have to write a little more code, because it needs a bit more options to call it.這就像你可以給自己買一個汽車發動機,你必須圍繞它制造一個帶有轉向、剎車等的整車......汽車制造商生產具有友好界面(踏板、方向盤)的完整汽車等)所以你不必自己做所有的事情.
It's like you would be able to buy yourself a car engine that you'd have to create a whole car around it with steering, brakes etc... Car manufacturers produce completed cars, with a friendly interface (pedals, steering wheel etc.) so you don't have to do it all yourself.
這篇關于XMLHttpRequest、jQuery.ajax、jQuery.post、jQuery.get 有什么區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!