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.