Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the use of jQuery.getJSON?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what is the use of jQuery.getJSON, I hope you will gain something after reading this article, let's discuss it together!

JQuery.getJSON (url [, data] [, success (data, textStatus, jqXHR)])

Description: use an HTTP GET request to load JSON-encoded data from the server.

Url

Type: String

A URL string containing the sending request

Data

Type: PlainObject

A string or Key/value key-value pair sent to the server.

Success (data, textStatus, jqXHR)

Type: Function ()

The callback function that is executed when the request is successful.

This is an abbreviation for the Ajax function, which is equivalent to:

$.ajax ({

DataType: "json"

Url: url

Data: data

Success: success

})

The data is appended to the URL of a query string and sent to the server. If the data parameter of the value is a normal object, it is converted to a string and encoded with URL before being appended to the URL.

In most cases, a callback function is specified after a successful request:

$.getJSON ('ajax/test.json', function (data) {

Var items = []

$.each (data, function (key, val) {

Items.push (''+ val +'')

})

$('', {

'class': 'my-new-list'

Html: items.join ('')

}). AppendTo ('body')

})

This example, of course, follows the structure of the JSON file:

{

"one": "Singular sensation"

"two": "Beady little eyes"

"three": "Little birds pitch by my doorstep"

}

Using this structure, this example iterates through the requested data, creates an unordered list, and appends it to the body.

Pass in the returned data in the success callback, usually a JSON structure defined by a JavaScript object or array, which is parsed using the $.parseJSON () method. It (success callback) also passes in the response status text.

In jQuery 1.5, the success callback function receives a "jqXHR" object (in jQuery 1.4, it receives a XMLHttpRequest object). However, since the JSONP form and cross-domain GET requests do not use XHR, in these cases the jqXHR and textStatus parameters are passed to the success (successful) callback is undefined.

Important: starting with jQuery 1.4, if the JSON file contains a syntax error, the request usually fails quietly. Therefore, frequent manual editing of JSON data should be avoided. The JSON syntax rules are stricter than the literal representation of JavaScript objects. For example, all strings in JSON, whether they are attributes or values, must be enclosed in double quotation marks.

After reading this article, I believe you have a certain understanding of "what is the use of jQuery.getJSON". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report