JSON和AJAX与jQuery的区别是什么?

时间:2022-08-23 11:34:27

I've heard that JSON serializes all the data, which stops me having problems client side in terms of cross-browser support etc..

我听说JSON序列化了所有的数据,这使我在跨浏览器支持等方面没有遇到客户端问题。

I've been using AJAX with jQuery and it seems easy, but I'm unsure of the differences,

我使用了AJAX和jQuery,这看起来很简单,但我不确定它们之间的区别,

I've read I can also use this to get the data:

我读过,我也可以用这个来获取数据:

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

Can anyone explain the difference between making a jQuery AJAX request using JSON and making a jQuery AJAX request without the json type?

有人能解释一下使用JSON发出jQuery AJAX请求与不使用JSON类型发出jQuery AJAX请求之间的区别吗?

Will the answer be ok for all browsers?

所有浏览器的答案都可以吗?

3 个解决方案

#1


28  

I think you are confusing the terms.

我认为你混淆了术语。

AJAX stands for Asynchronous Javascript and XML, which is a mechanism used to launch asynchronous HTTP requests to a server using JavaScript. Don't let the name fool you; there's no restriction on you only retrieving JavaScript or XML from this technique. You can quite happily return other data formats as well (HTML, plain text and JSON, to list a few).

AJAX代表异步Javascript和XML,这是一种使用Javascript向服务器启动异步HTTP请求的机制。不要让这个名字欺骗你;仅从此技术检索JavaScript或XML没有任何限制。您也可以很高兴地返回其他数据格式(HTML、纯文本和JSON,列出一些)。

JSON is just one of these formats. It's a data interchange format, where-as AJAX is a technique to communicate with a server after the initate page load has completed.

JSON只是其中一种格式。这是一种数据交换格式,在initate页面加载完成后,AJAX是一种与服务器通信的技术。

To answer your question on whether you need to specify the dataType; jQuery will best guess the response format (be it HTML or JSON etc), so you're usually fine to omit it.

回答是否需要指定数据类型的问题;jQuery将最好地猜测响应格式(无论是HTML还是JSON),因此您可以忽略它。

#2


4  

The dataType option simply changes what type of data jquery should expect from the server. It can be json, jsonp, html, text, xml, or any custom datatype that you define a converter for. They all work in all browsers.

dataType选项简单地更改了应该从服务器获取的数据jquery类型。它可以是json、jsonp、html、文本、xml或任何定义转换器的自定义数据类型。它们都在所有的浏览器中运行。

By default jQuery will try to detect what type of data is being returned if you do not supply a dataType option, however I find that it doesn't automatically detect very well.

默认情况下,如果不提供dataType选项,jQuery将尝试检测返回的数据类型,但我发现它不能很好地自动检测。

Edit:

编辑:

but what if i need to return an object? is basically the answer of a database consult... is it better to use json or only jquery?

但是如果我需要返回一个对象呢?基本上是数据库咨询的答案……使用json还是只使用jquery更好?

You can return an object in the form of html, xml, json, or jsonp. As long as it is in one of those formats, jQuery will be able to interpret it.

您可以以html、xml、json或jsonp的形式返回一个对象。只要它是那种格式,jQuery就能够解释它。

#3


0  

JQuery: It is a light weight Javascript Library.

JQuery:它是一个轻量级的Javascript库。

JSON - Stands for JavaScript Object Notation.

JSON -表示JavaScript对象表示法。

Jquery:It is created using JavaScript and you will be using the inbuilt functionalities from the library.

Jquery:它是使用JavaScript创建的,您将使用库中的内置函数。

Json: JSON is a text format that is completely language independent.

Json: Json是一种完全独立于语言的文本格式。

JQuery:It is a fast and minified JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

JQuery:它是一个快速、小型的JavaScript库,简化了HTML文档的遍历、事件处理、动画和Ajax交互,以实现快速的web开发。jQuery的设计目的是改变编写JavaScript的方式。

Json: If you want assign data to Your grid then it is possible with Json.

Json:如果你想给你的网格分配数据,那么Json是可能的。

#1


28  

I think you are confusing the terms.

我认为你混淆了术语。

AJAX stands for Asynchronous Javascript and XML, which is a mechanism used to launch asynchronous HTTP requests to a server using JavaScript. Don't let the name fool you; there's no restriction on you only retrieving JavaScript or XML from this technique. You can quite happily return other data formats as well (HTML, plain text and JSON, to list a few).

AJAX代表异步Javascript和XML,这是一种使用Javascript向服务器启动异步HTTP请求的机制。不要让这个名字欺骗你;仅从此技术检索JavaScript或XML没有任何限制。您也可以很高兴地返回其他数据格式(HTML、纯文本和JSON,列出一些)。

JSON is just one of these formats. It's a data interchange format, where-as AJAX is a technique to communicate with a server after the initate page load has completed.

JSON只是其中一种格式。这是一种数据交换格式,在initate页面加载完成后,AJAX是一种与服务器通信的技术。

To answer your question on whether you need to specify the dataType; jQuery will best guess the response format (be it HTML or JSON etc), so you're usually fine to omit it.

回答是否需要指定数据类型的问题;jQuery将最好地猜测响应格式(无论是HTML还是JSON),因此您可以忽略它。

#2


4  

The dataType option simply changes what type of data jquery should expect from the server. It can be json, jsonp, html, text, xml, or any custom datatype that you define a converter for. They all work in all browsers.

dataType选项简单地更改了应该从服务器获取的数据jquery类型。它可以是json、jsonp、html、文本、xml或任何定义转换器的自定义数据类型。它们都在所有的浏览器中运行。

By default jQuery will try to detect what type of data is being returned if you do not supply a dataType option, however I find that it doesn't automatically detect very well.

默认情况下,如果不提供dataType选项,jQuery将尝试检测返回的数据类型,但我发现它不能很好地自动检测。

Edit:

编辑:

but what if i need to return an object? is basically the answer of a database consult... is it better to use json or only jquery?

但是如果我需要返回一个对象呢?基本上是数据库咨询的答案……使用json还是只使用jquery更好?

You can return an object in the form of html, xml, json, or jsonp. As long as it is in one of those formats, jQuery will be able to interpret it.

您可以以html、xml、json或jsonp的形式返回一个对象。只要它是那种格式,jQuery就能够解释它。

#3


0  

JQuery: It is a light weight Javascript Library.

JQuery:它是一个轻量级的Javascript库。

JSON - Stands for JavaScript Object Notation.

JSON -表示JavaScript对象表示法。

Jquery:It is created using JavaScript and you will be using the inbuilt functionalities from the library.

Jquery:它是使用JavaScript创建的,您将使用库中的内置函数。

Json: JSON is a text format that is completely language independent.

Json: Json是一种完全独立于语言的文本格式。

JQuery:It is a fast and minified JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

JQuery:它是一个快速、小型的JavaScript库,简化了HTML文档的遍历、事件处理、动画和Ajax交互,以实现快速的web开发。jQuery的设计目的是改变编写JavaScript的方式。

Json: If you want assign data to Your grid then it is possible with Json.

Json:如果你想给你的网格分配数据,那么Json是可能的。