没有框架的javascript ajax请求
有谁知道如何在不使用 jQuery 等 javascript 框架的情况下制作跨浏览器的 ajax 请求功能?
Does anyone know how to make ajax request function that works cross-browser WITHOUT using a javascript framework like jQuery, etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XMLHttpRequest
对象实际上使用起来并不那么复杂。为了广泛兼容,您必须玩一些游戏来创建对象,但之后的简单操作就相当简单了。Microsoft 在 MSDN 页面上提供了
的示例XMLHttpRequest
,包括一个以跨浏览器方式创建对象的函数,支持早期版本的IE。这是他们的例子:我并不是说上面的例子是最佳实践(微软的 MSDN 例子似乎主要是由非常非常缺乏经验的工程师编写的),但它给了你一个起点。例如,上面要求响应状态为 200 才算成功,当然 HTTP 规范明确规定 200 <= n <= 299 范围内的任何内容都是“成功”。
The
XMLHttpRequest
object isn't actually all that complicated to use. To be broadly compatible, you have to play a bit of a game to create the object, but after that it's fairly straightforward for simple operations.Microsoft has examples on the MSDN page for
XMLHttpRequest
, including a function for creating the object in a cross-browser way that supports early versions of IE. Here's their example:I'm not suggesting the above exemplifies best practices (Microsoft seems to have their MSDN examples largely written by very, very inexperienced engineers), but it gives you a starting point. For instance, the above requires that the response status be 200 for success, where of course the HTTP specification is clear that anything the 200 <= n <= 299 range is "success".
我经常使用这种方法在现代浏览器中仅发送和接收 json (没有旧的 ie 浏览器)
i often use this method for sending and receiving only json in modern browsers (no old-ie's)