Skip to content

Instantly share code, notes, and snippets.

View vickonrails's full-sized avatar
🖥️
writing code

Victor Ofoegbu vickonrails

🖥️
writing code
View GitHub Profile
//server.js
const http = require('http'),
makeServer = function (request,response){
response.writeHead(200,{'Content-Type':'text/plain'});
response.write('Hello world');
response.end();
},
server = http.createServer(makeServer);
//server.js
const http = require('http'),
server = http.createServer((request,response)=>{
response.writeHead(200,{'Content-Type':'text/plain'});
response.write('Hello world');
response.end();
});
server.listen(3000,()=>{
console.log('Node server created at port 3000');
});
//server.js
const http = require('http'),
server = http.createServer();
server.on('request',(request,response)=>{
response.writeHead(200,{'Content-Type':'text/plain'});
response.write('Hello world');
response.end();
});
$.get(
"https://api.unsplash.com/photos/?client_id=your-client-id",
function(data){
console.log(data);
},
"json");
Parameter Type Default Description
url string - An address to fetch the data from.
data string - Specifies data to send to the server alongside the request
function(data,status,xhr) function - A function to handle the response when it arrives.
dataType string Automatic type casting Specifies the type of data to expect from the server. e.g json, text, xml.
$.ajax({
url : "https://api.unsplash.com/search/photos?query=home",
type: "GET",
async : true,
contentType: "application/x-www-urlencoded",
dataType : "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Client-ID your-client-id");
},
error : function(xhr,status,error){
Method Type Default Description
url string - The url to make the Async request to.
type string "GET" The type of request e.g GET or POST.
Async boolean true A value that specifies if the request be asynchronous or not. True is Asynchronous and default.
beforeSend(xhr) function - A function that is called before the request is sent.
cache boolean true Value indicating if the response be cached. True means the response be cached.
dataType string "json" Specifies the type of data to accept from server.
error(xhr,status,error) function - A function that is called when an error occurs during the request.
success(xhr,status,error) function - A function that is called when response arrives to the browser.
var request = new XMLHttpRequest();
request.onreadsystatechange = handleRequest;
request.open('GET','url',true);
request.send();
function handleRequest(){
console.log(request.responseText);
}
var request = new XMLHttpRequest();
request.addEventListener('progress',checkProgress);//fires when the readyState changes.
request.addEventListener('abort',abortMessage);// fires when the request is aborted.
request.addEventListener('error',errorMessage);// fires when an error occurs
request.addEventListener('load',handleRequest);// fires only when the request is successfull and data is sent back to the browser.
function handleRequest(){
console.log(request.responseText);
}
<!DOCTYPE html>
<html>
<head>
<title>Async request</title>
</head>
<body>
<p> click on the button to add an image to the page</p>
<button type="button">Get image</button>
<img src="" width="100%">
</body>