Javascript jQuery 是否只有 Ajax 部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4132163/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:02:11  来源:igfitidea点击:

Is there only Ajax part of jQuery?

javascriptjquery

提问by fgfgfgghjh

Is there only Ajax part of jQuery? The matter is I do not need the whole library but only its Ajax part (I need to include this part on several iframes).

jQuery 是否只有 Ajax 部分?问题是我不需要整个库,而只需要它的 Ajax 部分(我需要在几个 iframe 中包含这部分)。

回答by Neoaptt

Update 2016

2016 年更新

You can use this tool to build your own custom jQuery version.

您可以使用此工具构建您自己的自定义 jQuery 版本。

jQuery Builder

jQuery 生成器

As of jQuery 2.1.1

从 jQuery 2.1.1 开始

Full file sized unminified is: 241.55 Kb

未缩小的完整文件大小为:241.55 Kb

Ajax Only minified is: 49.60 Kb

Ajax 仅缩小为:49.60 Kb

That is a 5xreduction in size.

那是尺寸减少了5 倍

enter image description here

在此处输入图片说明

回答by Pekka

As Darin already says, it's all or nothing. JQuery's Ajax functions are closely intertwined with the rest of the functionality.

正如达林已经说过的,要么全有要么全无。JQuery 的 Ajax 函数与其他功能紧密地交织在一起。

There are a few other, stand-alone Ajax libraries around like Matt Kruse's Ajax toolbox- maybe that helps.

还有其他一些独立的 Ajax 库,比如Matt Kruse 的 Ajax 工具箱——也许这会有所帮助。

I would consider loading the full jQuery library. If you link to jQuery on a CDN,loading times will be minuscule.

我会考虑加载完整的 jQuery 库。如果您在 CDN 上链接到jQuery,加载时间将是微不足道的。

回答by Paul Hoenecke

As of jQuery 1.8 you can do it: LINK

从 jQuery 1.8 开始,你可以做到:LINK

回答by Daniel Waltrip

Another option would be to use the built-in fetchAPI provided by the browser.

另一种选择是使用fetch浏览器提供的内置API。

Here is an example snippet:

这是一个示例片段:

fetch('http://localhost:3000/users.json', {
  method: 'POST', 
  mode: 'cors', 
  redirect: 'follow',
  body: JSON.stringify({
     user: {
       firstName: 'john',
       lastName: 'doe'
     }
  }),
  headers: new Headers({ 'Content-Type': 'application/json' })
}).then(function() {
  /* handle response */
});

This blog postis a great introduction to the API and shows more use cases.

这篇博文很好地介绍了 API,并展示了更多用例。

fetchdoesn't have full cross-browser support yet (I think mainly IE and Safari are lacking), but there is polyfill that you can use until that day comes.

fetch还没有完全的跨浏览器支持(我认为主要是 IE 和 Safari 缺乏),但是在那天到来之前你可以使用 polyfill。

fetchpolyfill: https://github.com/github/fetch

fetchpolyfill:https: //github.com/github/fetch

Older browsers will also need a Promisepolyfill (one option, another option).

较旧的浏览器还需要一个Promisepolyfill(一个选项另一个选项)。

回答by William Isted

You can view standard javascript alternatives to jQuery at youmightnotneedjquery.com

您可以在youmightnotneedjquery.com查看 jQuery 的标准 javascript 替代品

For example the alternative to $.ajaxpostis:

例如,替代方案$.ajaxpost是:

var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);

And the alternative to $.ajaxgetis:

替代方案$.ajaxget是:

var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Success!
    var resp = request.responseText;
  } else {
    // We reached our target server, but it returned an error

  }
};

request.onerror = function() {
  // There was a connection error of some sort
};

request.send();

回答by Sista Fiolen

AMD user please read this, my answer is for building a single file.
Or just use this library: ded / reqwest(4 KB, min & gzip)

AMD 用户请阅读本文,我的答案是构建单个文件。
或者直接使用这个库:ded / reqwest(4 KB, min & gzip)



  1. Download source codeand run npn i

  2. Open /src/jquery.jsand remove any moudle you don't want, but keep "./exports/amd", "./exports/global"

    define([
        "./ajax",
        "./ajax/xhr",
        "./ajax/script",
        "./ajax/jsonp",
        "./exports/amd",
        "./exports/global"
    ], function (jQuery) {
        "use strict";
        return jQuery;
    });
    
  3. Run grunt custom:-sizzle

  4. Goto /distand take your build

  1. 下载源代码并运行npn i

  2. 打开/src/jquery.js并移除您不想要的任何模块,但保留"./exports/amd""./exports/global"

    define([
        "./ajax",
        "./ajax/xhr",
        "./ajax/script",
        "./ajax/jsonp",
        "./exports/amd",
        "./exports/global"
    ], function (jQuery) {
        "use strict";
        return jQuery;
    });
    
  3. grunt custom:-sizzle

  4. 转到/dist并获取您的构建

Modules in your build now:

现在构建中的模块:

  • core
  • deferred
  • ajax
  • 延期
  • 阿贾克斯

Size:

尺寸:

  • just build: 85 KB
  • build with min: 26 KB
  • build with min & gzip: 10 KB
  • 刚刚构建:85 KB
  • 最小构建:26 KB
  • 使用 min & gzip 构建:10 KB

回答by Sam

I've created a custom build of jQuery 1.7.1 here:

我在这里创建了 jQuery 1.7.1 的自定义版本:

https://github.com/dtjm/jquery/tree/ajaxonly

https://github.com/dtjm/jquery/tree/ajaxonly

回答by Noypi Gilas

YES, I just did mine, http://noypi-linux.blogspot.com/2013/05/build-jquery-with-ajax-only.html

是的,我刚刚做了我的, http://noypi-linux.blogspot.com/2013/05/build-jquery-with-ajax-only.html

you only need these files (resulting minified is about 30Kb):

您只需要这些文件(缩小后的大小约为 30Kb):

/d/dev/javascript/jquery/jquery/src/intro.js
/d/dev/javascript/jquery/jquery/src/core.js
/d/dev/javascript/jquery/jquery/src/callbacks.js
/d/dev/javascript/jquery/jquery/src/deferred.js
/d/dev/javascript/jquery/jquery/src/support.js
/d/dev/javascript/jquery/jquery/src/data.js
/d/dev/javascript/jquery/jquery/src/event.js
/d/dev/javascript/jquery/jquery/src/serialize.js
/d/dev/javascript/jquery/jquery/src/ajax.js
/d/dev/javascript/jquery/jquery/src/ajax/xhr.js
/d/dev/javascript/jquery/jquery/src/exports.js
 /d/dev/javascript/jquery/jquery/src/outro.js

回答by Adam Heath

If you really really want just the Ajax parts of jQuery you can get the code from their repository (https://github.com/jquery/jquery), glancing at it you would want to look at "ajax.js" and "core.js" in the "src" directory. You would then want to compile them together with the closure compiler or something.

如果你真的只想要 jQuery 的 Ajax 部分,你可以从他们的存储库 ( https://github.com/jquery/jquery) 中获取代码,浏览它你会想要查看“ajax.js”和“core” .js”在“src”目录中。然后,您可能希望将它们与闭包编译器或其他东西一起编译。

But as others stated, it would be a lot easier to just load it from one of the CDNs (jQuery, Google, Microsoft) which most users will have cached anyway.

但正如其他人所说,从大多数用户无论如何都会缓存的 CDN(jQuery、Google、Microsoft)之一加载它会容易得多。

回答by Darin Dimitrov

It's all or nothing. Of course jquery is open source and you could extract the part you are interested in in your own library (good luck with this). You may consider using a CDN which will ensure that most users will already have it cached in their browsers so you shouldn't be concerned about size.

要么全有要么全无。当然 jquery 是开源的,你可以在你自己的库中提取你感兴趣的部分(祝你好运)。您可以考虑使用 CDN,这将确保大多数用户已经将其缓存在他们的浏览器中,因此您不必担心大小。