Created by
request-method-name request-URI HTTP-version
GET /search?q=apple HTTP/1.1
HEAD /query.html HTTP/1.1
POST /index.html HTTP/1.1
field-name: field-value1[, field-value2, ...]
The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server
Host: google.com
User-Agent: curl/7.47.0
Accept: */*
cache-control: no-cache
cookie: OGPC=19022519-1:; OGP=-19022519:; OTZ=5933950_44_48_123900_44_436380;
Optional
HTTP-version status-code reason-phrase
HTTP/1.1 200 OK
HTTP/1.1 301 Moved Permanently
HTTP/1.1 404 Not Found
field-name: field-value1[, field-value2, ...]
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 11 Jun 2017 14:05:01 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Keep-Alive: timeout=20
X-Pingback: http://www.javelingroup.com/xmlrpc.php
Link: ; rel="https://api.w.org/"
Link: ; rel=shortlink
X-Cacheable: SHORT
Vary: Accept-Encoding,Cookie
Cache-Control: max-age=600, must-revalidate
X-Cache: HIT: 4
X-Pass-Why:
X-Cache-Group: normal
X-Type: default
The response message body contains the resource data requested
HTTP protocol defines a set of request methods, which a client can use to request a server resource.
https://duckduckgo.com/?q=http+get+request&t=hz&ia=web
HTTP Method | CRUD operation |
---|---|
GET | Read |
POST | Create |
PUT | Update/Replace |
PATCH | Update/Modify |
DELETE | Delete |
Asynchronous
JavaScript
And
XML
In reality, these days, the use of JSON prevails over XML, but for backward compatibility we do not say AJAJ
With AJAX, Web applications can send and retrieve data from a Web server asynchronously (in the background) without refreshing the existent page
let dom={
output: document.querySelector('.output'),
getDataBtn: document.querySelector('.getDataBtn'),
};
const dataURL = '/data/heyYouLirics.txt';
let fetchDataByXHR = function(){
// create a XMLHttpRequest object
let xhr = window.XMLHttpRequest ? new XMLHttpRequest() :
new ActiveXObject("Microsoft.XMLHTTP");
// initializes the request:
xhr.open("GET", dataURL);
// EventHandler, that will be fired each time when the xhr state changes
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// finally do something with content:
console.log(this.responseText);
dom.output.innerHTML = this.responseText;
};
};
// sends the request:
xhr.send();
};
dom.getDataBtn.addEventListener('click', function(){
fetchDataByXHR();
});
fetch(url)
.then(function (response){
return response.text();
})
.then(function (responseText){
// do something with responseText
nodes.output.innerHTML = responseText;
})
catch
function) only when a network error occurs.
fetch(url)
.then(function (response) {
if( response.text){
return response.text();
}else{
throw new Error("No response.text")
}
})
.then(function (responseText) {
// renderHTML(responseText);
nodes.output.innerHTML = responseText;
})
.catch( function(error){
console.error('Upss, an error occurred');
console.error(error.message);
});
response
interface
<head>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<button class="getDataBtn">Get data</button>
<div class="output"></div>
</body>
$(".getDataBtn").click(function(){
$.get(dataURL, function(data, status){
// do something with data:
$(".output").html(data);
});
});
<head>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<button class="getDataBtn">Get data</button>
<div class="output"></div>
</body>
$(".getDataBtn").click(function(){
$(".output").load(dataURL);
});
{
'fruits': ['apple', 'orange', 'banana'],
}
apple
orange
banane
key:value
pairs, separated by comma, delimited by curly braces. The keys must be strings.
{
"userName": "Ada",
"userAge": 24,
"skills": [
"HTML", "CSS", "JavaScript"
],
"tasks": [
{
"id": 1,
"title":"Learn Sass",
"completed": true
},
{
"id": 1,
"title":"Learn React",
"completed": false
}
]
}
You can check your JSON online: jsonformatter.curiousconcept.com or use a VSCode extension.
var data = JSON.parse(jsonDataStr);
var jsonDataStr = JSON.stringify(obj);
These slides are based on
customised version of
framework