Home Web Front-end JS Tutorial 2 ways to obtain json data asynchronously with jQuery_jquery

2 ways to obtain json data asynchronously with jQuery_jquery

May 16, 2016 pm 04:38 PM
jquery json data asynchronous Way Obtain

The examples in this article describe two ways for jQuery to obtain json data asynchronously, which is of great practical value in web program development. Share it with everyone for your reference. The specific method is as follows:

Generally speaking, there are two ways for jQuery to asynchronously obtain json data, one is the $.getJSON method and the other is the $.ajax method. This article will use these two methods to asynchronously obtain json data and then append it to the page.

Create the data.json file in the root directory:

{
  "one" : "Hello",
  "two" : "World"
}
Copy after login

1. Obtain json data through the $.getJSON method

  <script src="Scripts/jquery-2.1.1.min.js"></script>
  <script type="text/javascript">
    $(function() {
      $.getJSON('data.json', function(data) {
        var items = [];
        $.each(data, function(key, val) {
          items.push('<li id="' + key + '">' + val + '</li>');
        });
        $('<ul/>', {
          'class': 'list',
          html: items.join('')
        }).appendTo('body');
      });
    });
  </script>
Copy after login

2. Obtain json data through $.ajax method

  <script src="Scripts/jquery-2.1.1.min.js"></script>
  <script type="text/javascript">
    $(function() {
      $.ajax({
        url: 'data.json',
        dataType: 'json',
        success: function(data) {
          var items = [];
          $.each(data, function(key, val) {
            items.push('<li id="' + key + '">' + val + '</li>');
          });
          $('<ul/>', {
            'class': 'list',
            html: items.join('')
          }).appendTo('body');
        },
        statusCode: {
          404: function() {
            alert("没有找到相关文件~~");
          }
        }
      });
    });
  </script>

Copy after login

Summary: Using the $.getJSON method and the $.ajax method can achieve the same effect. However, if you want to have more detailed control over the asynchronous acquisition process, it is recommended to use the $.ajax method.

I believe that what is described in this article has certain reference value for everyone’s jQuery programming.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

How to install dual SIM on Realme 12 Pro? How to install dual SIM on Realme 12 Pro? Mar 18, 2024 pm 02:10 PM

How to install dual SIM on Realme 12 Pro?

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Use jQuery to modify the text content of all a tags

Advanced Guide to Python asyncio: From Beginner to Expert Advanced Guide to Python asyncio: From Beginner to Expert Mar 04, 2024 am 09:43 AM

Advanced Guide to Python asyncio: From Beginner to Expert

How to get free chat times in Taqu How to get free chat times in Taqu How to get free chat times in Taqu How to get free chat times in Taqu Mar 12, 2024 pm 03:58 PM

How to get free chat times in Taqu How to get free chat times in Taqu

How to get Huang Rong in the mobile game Shooting Condor How to get Huang Rong in the mobile game Shooting Condor Mar 27, 2024 pm 10:36 PM

How to get Huang Rong in the mobile game Shooting Condor

See all articles