Jquery 中 .load() 和 .ajax() 函数的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5250630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between .load() and .ajax() functions in Jquery
提问by Aditya Shukla
Possible Duplicate:
difference between $(“#id”).load and $.ajax?
I am using .ajax() for an asynchronous call in my code ,while reading about .load() it looks like it does the same thing.What is the difference between both the methods?
我在我的代码中使用 .ajax() 进行异步调用,在阅读 .load() 时,它看起来像是在做同样的事情。这两种方法有什么区别?
回答by Baz1nga
$.ajax() is the most configurable one, where you get fine grained control over HTTP headers and such. You're also able to get direct access to the XHR-object using this method. Slightly more fine-grained error-handling is also provided. Can therefore be more complicated and often unecessary, but sometimes very useful. You have to deal with the returned data yourself with a callback.
$.ajax() 是最可配置的,您可以在其中对 HTTP 标头等进行细粒度控制。您还可以使用此方法直接访问 XHR 对象。还提供了更细粒度的错误处理。因此可能更复杂,通常是不必要的,但有时非常有用。您必须通过回调自己处理返回的数据。
.load() is similar to $.get() but adds functionality which allows you to define where in the document the returned data is to be inserted. Therefore really only usable when the call only will result in HTML. It is called slightly differently than the other, global, calls, as it is a method tied to a particular jQuery-wrapped DOM element. Therefore, one would do: $('#divWantingContent').load(...)
.load() 与 $.get() 类似,但增加了允许您定义返回数据插入文档中的位置的功能。因此,只有在调用只会导致 HTML 时才真正可用。它的调用方式与其他全局调用略有不同,因为它是一种绑定到特定 jQuery 包装的 DOM 元素的方法。因此,可以这样做: $('#divWantingContent').load(...)
It should be noted that all $.get(), $.post(), .load() are all just wrappers for $.ajax() as it's called internally.
应该注意的是,所有 $.get()、$.post()、.load() 都只是 $.ajax() 的包装器,因为它在内部被调用。
回答by Kranu
$.load
puts the contents of a URL into a specific HTML element. Unlike $.ajax
, it has a much more specific purpose (high-level).
$.load
将 URL 的内容放入特定的 HTML 元素。与 不同$.ajax
,它有一个更具体的目的(高级)。
$.ajax
, on the other hand, is the low-level function. It accepts many more arguments, and as such, can be set to mimic the behavior of $.load
, or many other high-level AJAX functions such as $.get
and $.post
.
$.ajax
,另一方面,是低级函数。它接受更多参数,因此可以设置为模仿$.load
或许多其他高级 AJAX 函数(例如$.get
和 )的行为$.post
。
回答by Dunhamzzz
If you append .load()
to an element, jQuery will put the contents of the ajax call in the element. It's kinda like a alias of .ajax()
which returns the data for you instead of having to use a callback.
如果您附加.load()
到一个元素,jQuery 会将 ajax 调用的内容放入该元素中。它有点像别名,.ajax()
它为您返回数据,而不必使用回调。