[英]jQuery ajax() vs get()/post()
Let's say I want to execute a PHP script. 假设我想执行PHP脚本。 Which way is better?
哪种方式更好?
This: 这个:
$.ajax({
type: "GET",
url: "php-script.php",
dataType: "script"
});
Or this: 或这个:
$.get("php-script.php", function(data) { });
In this case, I'd say $.get
, as it's immediately clear what the type of request is. 在这种情况下,我会说
$.get
,因为它会立即清楚请求的类型。 At any rate, it's just shorthand for the larger and more option-ified ajax call, and converting between the two is trivial in the worst case. 无论如何,它只是更大和更多选择性的ajax调用的简写,并且在最坏的情况下在两者之间进行转换是微不足道的。
If you think that you'll need fancy $.ajax
options, use $.ajax
. 如果您认为您需要花哨的
$.ajax
选项,请使用$.ajax
。 If you don't use the convenience methods jQuery provides, such as .load
, $.get
, etc. 如果你不使用jQuery提供的便利方法,例如
.load
, $.get
等。
$.get()
is just a shortcut for an $.ajax()
call of type "GET". $.get()
只是“GET”类型的$.ajax()
调用的快捷方式。
To Elaborate $.get()
just calls $.ajax()
in the background. 要精心设计
$.get()
只需在后台调用$.ajax()
。 Its a shortcut kind of like what $(function(){})
is to $(document).ready(function(){})
. 它是
$(function(){})
对$(document).ready(function(){})
的快捷方式。 Common use cases for $.get
is for a quick and simple get, $.ajax
should be used if you need to do something a bit more complex or if you need the extra flexibility. $.get
常见用例是快速简单的获取,如果你需要做一些更复杂的事情或需要额外的灵活性,应该使用$.ajax
。
$.get()
/ $.post()
don't support beforeSend
like $.ajax()
does. $.get()
/ $.post()
不像$.ajax()
那样支持beforeSend
。 So you can't for example display a spinner while loading directly. 因此,您无法在直接加载时显示微调器。 But you can do this with
.ajaxStart()
. 但是你可以用
.ajaxStart()
来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.