XMLHttpRequest、jQuery.ajax、jQuery.post、jQuery.get 有什么区别

2022-01-15 00:00:00 xmlhttprequest jquery ajax

如何确定哪种方法最适合某种情况?谁能提供一些示例来了解功能和性能方面的差异?

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.getjQuery.post 只能发出 GET 和POST 请求.如果你不知道这些是什么,你应该检查 HTTP 协议 并了解一点.这两个函数在内部使用 jQuery.ajax,但它们使用您不必自己设置的特定设置,因此与使用 jQuery.ajax 相比,简化了 GET 或 POST 请求.GET 和 POST 是最常用的 HTTP 方法(与 DELETE、PUT、HEAD 甚至其他很少使用的外来方法相比).

      jQuery.get and jQuery.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 use jQuery.ajax but they use particular settings that you don't have to set yourself thus simplifying GET or POST request compared to using jQuery.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.

相关文章