Home > Web Front-end > JS Tutorial > body text

jQuery.getJSON(url, [data], [callback]) Basic introduction

巴扎黑
Release: 2017-07-03 09:57:43
Original
1093 people have browsed it

Return value: XMLHttpRequestjQuery.getJSON(url, [data], [callback])

Overview

Load JSON data via HTTP GET request.

In jQuery 1.2, you can load JSON data from other domains by using the callback function in JSONP form, such as "myurl?callback=?". jQuery will automatically replace ? with the correct function name to execute the callback function. Note: The code after this line will be executed before this callback function is executed.

Parameters
urlString

Sending request address.

data (Optional)Map

Key/value parameters to be sent.

callback (Optional)Function

Callback function when loading is successful.

Example
Description:

Load 4 latest pictures of cats from the Flickr JSONP API.

HTML code:
<p id="images"></p>
Copy after login
jQuery code:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images");
    if ( i == 3 ) return false;
  });
});
Copy after login
Description:

Load JSON data from test.js and display a name field in the JSON data data.

jQuery Code:
$.getJSON("test.js", function(json){
  alert("JSON Data: " + json.users[3].name);
});
Copy after login
Description:

Load JSON data from test.js, append parameters, and display a name field data in the JSON data.

jQuery Code:
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){
  alert("JSON Data: " + json.users[3].name);
});
Copy after login


This article was written by the author: Chen Xizhang on 2009/7/2 8:27:43 Published at: http://www.cnblogs.com/chenxizhang/
The copyright of this article belongs to the author and can be reproduced. However, this statement must be retained without the author's consent, and a link to the original text must be provided in an obvious position on the article page, otherwise We reserve the right to pursue legal liability.
For more blog posts, as well as the author’s complete statement on blog citation and cooperation policies, please refer to the following site: Chen Xizhang’s Blog Center

The above is the detailed content of jQuery.getJSON(url, [data], [callback]) Basic introduction. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!