JSON APIs and Ajax

Source: Internet
Author: User

1.

Use jquery to bind a point-and-click event.

The $(document).ready() code in this function will only run once when our page is loaded, making sure that all the DOM on the page before executing JS is ready.

$(document).ready()add an event to the function click . The code is as follows:

$ ("#getMessage"). On ("click", Function () {

});

2.

The application Interface (API) comes in handy when you need to dynamically change the page based on the data returned by the server.

Remember, the api--application interface (application programming Interface) is a tool for communicating with each other on the computer.

Many web site application interfaces (APIs) are transmitted through a data called JSON format, which is shorthand for JavaScript Object notation.

In fact, if you have ever created a JS object, you have already used this data format, JSON is a very concise data format.

It usually behaves in two forms, one for a single object and one for multiple objects

A single object resembles the following:
{name:‘盖伦‘,advantage:‘单挑无敌‘}

Multiple objects are similar to:
[{name:‘盖伦‘,advantage:‘单挑无敌‘},{name:‘诺克‘,advantage:‘上单霸主‘}]

The combination of each object property and property value is the "key-value pair (Key-value pairs)" that we often hear.

Let's take the data from the previous Cat diagram API.

You should add the following code to your click event:

$.getjson ("/json/cats.json", function (JSON) {
$ (". Message"). HTML (json.stringify (JSON));
});

Display results: [{"id": 0, "ImageLink": "/images/funny-cat.jpg", "codenames": ["Juggernaut", "Mrs. Wallace", "Buttercup"]},{"id" : 1, "ImageLink": "/images/grumpy-cat.jpg", "codenames": ["Oscar", "Scrooge", "Tyrion"]},{"id": 2, "ImageLink": "/images /mischievous-cat.jpg "," codenames ": [" The Doctor "," Loki "," Joker "]}]

3.

Already got the data from the JSON API, now show them to our HTML page.

Here, we use .forEach() functions to iterate through the JSON data written to the HTMLL variable.

First we define an HTML variable,
var html = "";

We then use the .forEach() function to iterate through the JSON data to write to the HTML variable, and finally to display the HTML variables in our page.

The code for the entire process is as follows:

Json.foreach (function (val) {
var keys = Object.keys (val);
HTML + = "<div class = ' cat ' >";
Keys.foreach (function (key) {
HTML + = "<b>" + key + "</B>:" + Val[key] + "<br>";
});
HTML + = "</div><br>";
});

Show Results:

id:0
Imagelink:/images/funny-cat.jpg
Codenames:juggernaut,mrs. Wallace,buttercup

Id:1
Imagelink:/images/grumpy-cat.jpg
Codenames:oscar,scrooge,tyrion

Id:2
Imagelink:/images/mischievous-cat.jpg
Codenames:the Doctor,loki,joker

4.

In the JSON array obtained from the previous lesson, each object contains a imageLink key value pair that is a key (key), with the URL of the cat's picture as the value (value).

When we traverse these objects, we use imageLink properties to display img the image of the element.

The code is as follows:

html += "

5.

If we don't want to show all the images we get from the JSON API, we can do a filter before we iterate.

We filter out the image with the value of "id" key 1.

The code is as follows:

JSON = Json.filter (function (val) {
Return (val.id!== 1);
});

6.

We also have access to navigator our current location via our browser geolocation .

The location information includes longitude longitude and latitude latitude .

You will see a prompt to be allowed to get the current location. Whether you choose to allow or prohibit, as long as the code is correct, this can pass.

If you choose to allow, you will see that the text on the right side of your phone is the latitude and longitude of your current location.

The code is as follows:

if (navigator.geolocation) {
Navigator.geolocation.getCurrentPosition (function (position) {
$ ("#data"). HTML ("Latitude:" + position.coords.latitude + "<br>longitude:" + position.coords.longitude);
});
}

JSON APIs and Ajax

Related Article

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.