在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:sei-relativity/lesson-w04d03-ajax-axios开源软件地址:https://github.com/sei-relativity/lesson-w04d03-ajax-axios开源编程语言:开源软件介绍:AJAX + AxiosHTTP RecapHow The Internet Works in 5 Minutes Let's define a few terms:
The Internet is a telecommunications network. All telecommunications networks—from telegraphs, through phone and radio, and to teletype—facilitate transmission of data from one point to another. The World Wide Web (WWW, or simply "web") is a distributed, world-wide collection of documents transmitted over the HTTP protocol.
VerbsURLs reveal the identity of the particular host with which we want to communicate, but the action that should be performed on the host is specified via HTTP verbs (or methods, as they are properly called). Of course, there are several actions that a client would like the host to perform. HTTP has formalised on a few that capture the essentials that are universally applicable for all kinds of applications. These request verbs are:
The above four verbs are the most popular, and most tools and frameworks explicitly expose these request verbs. PUT and DELETE are sometimes considered specialised versions of the POST verb, and they may be packaged as POST requests with the payload containing the exact action: create, update or delete. Examples:
https://www.restapitutorial.com/lessons/httpmethods.html APIIn computer science API application programming interface is quite a broad term which can be confusing. However, The term now commonly refers to web URLs that can be accessed for raw data. When we think about APIs within the context of web application development we can simply think about it as a website that returns JSON instead of HTML so that a computer can use the data to do something instead of a human. It allows a programme to interface with a website over the internet so that it can use that websites' data. Q: Why use api?
Many web sites have their own data, but they can pull in other data. For example, many news sites have a weather widget. This widget gets its data from a weather resource. There are many APIs that can be used by individuals and companies. Some are totally free, some are available for a small fee, and some are really expensive. Let's check out a few API's: PostmanPostman is a tool to test and build APIs. It's super helpful to test requests that will require a form ( API RequestsCREATE/POST DOCSCREATE WITH A FORMCREATE WITH RAW JSONJSONJSON stands for JavaScript Object Notation. It is a lightweight way for storing and transporting data. JSON is:
JSON is not:
JSON does not support:
JSON Example{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
} We can add a chrome extension to make JSON easier to read.
AJAXAJAX = Asynchronous JavaScript And XML. AJAX is not a programming language. AJAX just uses a combination of:
AJAX allows developers to:
AJAX is a misleading name. AJAX applications might use XML to transport data, but it is equally common to transport data as plain text or JSON text. AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. XML and JSONBack in the day, APIs used to send data in XML format,which is where the X in AJAX comes from. However, JSON has become the predominant way to send data over the internet. Even though JSON has become the preferred method of data transfer, no one seems in a hurry to start calling AJAX AJAJ. When we will use AJAX, we will be sending and receiving JSON. Let's take a look at a few sites that use AJAX:
What would happen if we disabled javascript and tried to use those sites? We make AJAX requests with
AxiosWe will be using Axios for our AJAX requests. Axios is a very popular library and we can use it in the browser and with node. Request
Get Request Example axios({
method: 'get',
url: 'https://swapi.co/api/people/1'
}); Post Request Example axios({
method: 'post',
url: 'https://swapi.co/api/people/1',
data: {
firstName: 'brunos',
lastName: 'ilovenodejs'
}
}); Response Object
Error Object
Handling ResponsesSince an AJAX call is asynchronous, we need to handle its response in a particular way. You may have already used some asynchronous javascript with Let's take a look at how asynchronous javascript works! To work with asynchronous javascript, we are going to use promises and a promise chain. JavaScript PromisesRemember, JavaScript is Asynchronous. It doesn't wait for one thing to finish before moving on to the next thing. So we must figure out a way to wait for our Axios call to finish getting all the data before we start working with it. To work with asynchronous javascript, we are going to use A After calling the initial function (in our case
// Example 1
axios({
url: 'https://cataas.com/cat',
method: 'get',
})
.then()
.catch() // .then and .catch are chained at the end of the request It is easier to ready if we place them on the next line axios({
url: 'https://cataas.com/cat',
method: 'get',
})
.then() // .then wants a function to run if the request is succesful
.catch() // .catch wants a function to run if the request is succesful The axios({
url: 'https://cataas.com/cat',
method: 'get',
})
.then(doGoodStuff)
.catch(doErrorStuff) We often use anonymous, fat arrow functions. axios({
url: 'https://cataas.com/cat',
method: 'get',
})
.then(() => {
// code for if the request succeeds
})
.catch(()=>{
// code for if the request fails
})
axios({
url: 'https://cataas.com/cat',
method: 'get',
})
.then((response) => {
// code for if the request succeeds
console.log(response)
})
.catch((error)=>{
// code for if the request fails
console.log(error)
}) |
2022-08-15
2022-08-17
2022-09-23
2023-10-27
2022-08-18
请发表评论