本文介绍了jQuery中.load()和.ajax()函数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用.ajax()进行异步调用,而在阅读有关.load()时,看起来好像是做同样的事情.这两种方法之间有什么区别?

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()是最可配置的,您可以在其中对HTTP标头等进行精细控制.您还可以使用此方法直接访问XHR对象.还提供了更细粒度的错误处理.因此可能会更复杂并且通常是不必要的,但有时非常有用.您必须自己通过回调处理返回的数据.

$.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.

.load()与$ .get()类似,但增加了功能,可让您定义返回的数据将在文档中插入的位置.因此,仅在调用仅会导致HTML时才真正可用.它的调用方式与其他全局调用方式略有不同,因为它是绑定到特定的jQuery包装的DOM元素的方法.因此,可以这样做:$('#divWantingContent').load(...)

.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(...)

应该注意,所有$ .get(),$.post()和.load()都只是内部调用$ .ajax()的包装.

It should be noted that all $.get(), $.post(), .load() are all just wrappers for $.ajax() as it's called internally.

这篇关于jQuery中.load()和.ajax()函数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:09