$ .getScript()和$ .get()之间的区别 [英] Difference between $.getScript() and $.get()

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

问题描述

我试图了解jQuery中的 $.getScript 函数和 $.get 之间的区别是什么.

I am trying to understand what are the differences between $.getScript function in jQuery and the $.get.

根据文档: http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.getScript/

给我的感觉是,使用 $.getScript ,您可以将数据发送到服务器(与使用 $.get 函数一样),但无法获得来自服务器的数据(可以使用 $.get 函数).但是它在 $.getScript 的文档中显示,在第一个示例的下面几行中,您还可以通过 console.log(data)行获取数据.//返回的数据.

It gave me the feeling that with the $.getScript you can send data to the server (as with the $.get function) but you can't get data from the server (which you can with the $.get function). But it shows in the documentation of $.getScript, some lines below in the first example, that you can get also data with the line console.log(data); //data returned.

那有什么区别?是使用 $.getScript 只能调用js脚本,而使用 $.get 可以调用任何文件吗?使用一个功能而不是另一个功能的限制/好处是什么?

So what is the differences? Is it that with $.getScript you can call only js scripts and with $.get you can call whatever file? What are the restrictions / benefits of using one function instead of the other?

推荐答案

这两个都是 ajax 函数调用的快捷方式. jQuery.get 等效于:

Both of these are shortcuts to ajax function call. jQuery.get is equivalent to:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

jQuery.getScript 等效于:

$.ajax({
  url: url,
  dataType: "script",
  success: success
});

很容易看到 jQuery.get 可以获取任何响应类型(脚本,xml,json,脚本或html-默认为html),以及 getScript 仅限脚本".

It is easy to see that jQuery.get can obtain any response type (script, xml, json, script, or html - by default html), and getScript is limited to "script".

简而言之, getScript 用于动态执行外部JavaScript,并且 get 是通用函数,通常用于根据参数接收数据通过了.但是,也可以在 getScript (在URL中)中传递参数,但是不会很常见,因为大多数脚本都是静态的.最后, getScript 中的回调可用于在执行脚本后执行最终语句(例如,在加载脚本后使用一些库函数).

In short, getScript is used to dynamically execute external JavaScript, and get is general purpose function usually used to receive data according to params passed. However, it is also possible to pass params in getScript (in URL) but that will be not common, because most scripts are static. Finally callback in getScript can be used to execute final statements after our script was executed (for example, use some library function after loading it).

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

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