不使用原生XMLHttpRequest的原因 - 为什么$ .ajax是强制性的? [英] Reasons not to use native XMLHttpRequest - why is $.ajax mandatory?

查看:104
本文介绍了不使用原生XMLHttpRequest的原因 - 为什么$ .ajax是强制性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序:

I'm writing an app that:


  1. 绝对依赖于通过 Javascript向服务器发送数据

  2. 代码中不得包含 JQuery 库。

  1. absolutely relies on sending data to server via Javascript
  2. must not have JQuery library included in code.

(我不知道我的代码是否会包含在已经有jquery lib的网页中,我不知道是否有会是正确的版本,我不知道是否会有 JQuery 冲突等......)

(I don't know if my code will be included in web pages that already have jquery lib, I do not know if there's gonna be right version, I do not know if there is gonna be JQuery conflict and so... )

我必须继续本地 JS 功能 XMLHttpRequest(旧IE的ActiveX)方法,这很简单,但比我看到警告:

I must relay on native JS functionality XMLHttpRequest(ActiveX for older IE's) methods, which is simple but than I saw warning:


如果没有jQuery,AJAX编码可能有点棘手!

"Without jQuery, AJAX coding can be a bit tricky!

编写常规的AJAX代码可能有点棘手,因为不同的
浏览器具有不同的AJAX实现语法。这意味着
你必须编写额外的代码来测试不同的浏览器。
但是, jQuery团队已经为我们处理了这个问题,因此我们只需要一行代码即可以
编写AJAX功能。

Writing regular AJAX code can be a bit tricky, because different browsers have different syntax for AJAX implementation. This means that you will have to write extra code to test for different browsers. However, the jQuery team has taken care of this for us, so that we can write AJAX functionality with only one single line of code."

对此有什么'棘手'?我怎么可能做错了?据我所知

What is 'tricky' about that? What am I possible doing wrong? As far as I know

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

只是不同浏览器的额外代码,我的xmlhttp var应该是从现在开始的准备在 IE5 IE6 IE7 + 中完成工作, Firefox Chrome Opera Safari

is only extra code for different browsers and my xmlhttp var should be from now on ready to do the job in IE5, IE6, IE7+, Firefox, Chrome, Opera, Safari.

高于跨浏览器代码调整我应该做什么或者还有什么我应该关注的当使用原生 XMLHttpRequest 对象?

Is above cross-browser code adjustment all I should do or is there anything else I should take care about when using native XMLHttpRequest object?

推荐答案

那是只有跨浏览器支持才需要的东西,除非你需要支持真正的旧IE(显然使用不同的ActiveXObject)。

That's the only thing necessary for cross-browser support unless you need to support really old IEs (which apparently use a different ActiveXObject).

然而,使用例如jQuery提供了许多额外的优点:

However, using e.g. jQuery provides a bunch of additional advantages:


  • 单个函数调用,没有不必要的变量使你的范围变得混乱。

  • 将对象自动序列化为GET / POST参数。当你可以简单地传递一个对象时,为什么要手动(你需要正确编码值!)?

  • 自动解析响应,例如:在JSON的情况下,你得到一个对象,而不仅仅是你需要手动解析的字符串。

  • 常见事件的好回调(成功/失败/完成)

  • 支持插件的可插拔架构IE中的CORS XDomainRequest。

  • Single function call, no unnecessary variables cluttering your scope.
  • Automated serialization of objects into GET/POST arguments. Why do that manually (you need to encode the values properly!) when you can simply pass an object?
  • Automated parsing of the response, e.g. in case of JSON you get an object and not just a string you need to parse manually.
  • Nice callbacks for common events (success/failure/finished)
  • Pluggable architecture to support e.g. XDomainRequest for CORS in IE.

因为你提到你不能使用jQuery,因为你的应用程序将被嵌入到可能与之冲突的另一个站点中它:您可以通过包含jQuery然后使用 $ .noConflict(true)来恢复旧的 $ jQuery 变量。要在你的代码中使用它,你可以这样写:

Since you mention that you cannot use jQuery because your app will be embedded in another site that might conflict with it: You can avoid this by including jQuery and then using $.noConflict(true) to restore both the old $ and jQuery variables. To use it in your code you then write it like this:

(function($) {

})(jQuery.noConflict(true));

这篇关于不使用原生XMLHttpRequest的原因 - 为什么$ .ajax是强制性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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