$.ajax() 和 $.get() 和 $.load() 的区别 [英] Difference between $.ajax() and $.get() and $.load()

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

问题描述

$.ajax()$.get()$.load() 有什么区别?

What is the difference between $.ajax() and $.get() and $.load()?

在什么条件下使用哪个更好?

Which is better to use and in what conditions?

推荐答案

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

$.get() 只是 $.ajax() 的简写,但抽象了一些配置,为它隐藏的内容设置合理的默认值.将数据返回给回调.它只允许 GET 请求,因此带有用于类似抽象的 $.post() 函数,仅用于 POST

$.get() is just a shorthand for $.ajax() but abstracts some of the configurations away, setting reasonable default values for what it hides from you. Returns the data to a callback. It only allows GET-requests so is accompanied by the $.post() function for similar abstraction, only for POST

.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 的 Ajax 文档中的更多详细信息:http://api.jquery.com/category/ajax/

More details in the Ajax-documentation of jQuery: http://api.jquery.com/category/ajax/

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆