JQuery provides two asynchronous methods for obtaining json data: jqueryjson

Source: Internet
Author: User

JQuery provides two asynchronous methods for obtaining json data: jqueryjson

This example describes two methods for jQuery to asynchronously obtain json data, which is of great practical value in web application development. Share it with you for your reference. The specific method is as follows:

Generally, jQuery asynchronously obtains json data in two ways: $. getJSON and $. ajax. In this article, we will use these two methods to asynchronously obtain json data and then append it to the page.

Create a data. json file in the root directory:

{  "one" : "Hello",  "two" : "World"}
 

1. Get 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>

 

2. Get json data through the $. 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 ("no relevant file found ~~ ") ;}}}) ;}); </Script>

Summary:The $. getJSON method and $. ajax method both achieve the same effect. However, if you want to have more detailed control over the asynchronous acquisition process, we recommend that you use the $. ajax method.

I believe this article provides some reference value for jQuery program design.


When the jquery post method is used for asynchronous access, the returned json data cannot be parsed?

1. If you specify datatype: 'json', it will not work if the background is not converted to json format.
2. parse is used to parse json objects from a string.
3. If the background returns an object in json format, you can directly access the object through the data point attribute.
If the backend is list to josn, you can use the data [0] Point attribute. Of course, your ype is json.

How can I use jquery to obtain the same name input and encapsulate it in json for asynchronous submission using ajax?

Hello!

Try the following code:
Var jsonObj = $ ("input [name = 'specify the name value you want ']"). serializeArray ();
In this way, a json object is obtained.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.