Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ajax-example
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robin Keskisärkkä
ajax-example
Commits
885ed4d2
Commit
885ed4d2
authored
2 years ago
by
Robin Keskisärkkä
Browse files
Options
Downloads
Patches
Plain Diff
add example that uses JSON body in POST requests
parent
f9f91cf3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
public/index.html
+18
-12
18 additions, 12 deletions
public/index.html
server.js
+8
-3
8 additions, 3 deletions
server.js
with
26 additions
and
15 deletions
public/index.html
+
18
−
12
View file @
885ed4d2
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
function
getUsingXMLHttpRequest
(){
function
getUsingXMLHttpRequest
(){
let
xhttp
=
new
XMLHttpRequest
();
let
xhttp
=
new
XMLHttpRequest
();
xhttp
.
onload
=
()
=>
{
xhttp
.
onload
=
()
=>
{
document
.
getElementById
(
'
1
'
).
innerHTML
=
xhttp
.
responseText
document
.
getElementById
(
'
1
'
).
innerHTML
=
JSON
.
parse
(
xhttp
.
responseText
).
counter
}
}
xhttp
.
open
(
'
GET
'
,
'
/counter
'
,
true
);
xhttp
.
open
(
'
GET
'
,
'
/counter
'
,
true
);
xhttp
.
send
();
xhttp
.
send
();
...
@@ -16,31 +16,37 @@
...
@@ -16,31 +16,37 @@
function
postUsingXMLHttpRequest
(){
function
postUsingXMLHttpRequest
(){
let
xhttp
=
new
XMLHttpRequest
();
let
xhttp
=
new
XMLHttpRequest
();
xhttp
.
onload
=
()
=>
{
xhttp
.
onload
=
()
=>
{
document
.
getElementById
(
'
2
'
).
innerHTML
=
xhttp
.
responseText
console
.
log
(
xhttp
.
responseText
)
document
.
getElementById
(
'
2
'
).
innerHTML
=
JSON
.
parse
(
xhttp
.
responseText
).
counter
}
}
xhttp
.
open
(
'
POST
'
,
'
/counter
'
,
true
);
xhttp
.
open
(
'
POST
'
,
'
/counter
'
,
true
);
xhttp
.
send
();
xhttp
.
setRequestHeader
(
"
Content-Type
"
,
"
application/json
"
);
xhttp
.
send
(
JSON
.
stringify
({
'
add
'
:
1
}));
}
}
function
getUsingFetch
(){
function
getUsingFetch
(){
fetch
(
'
/counter
'
,
{
method
:
'
GET
'
}).
then
(
resp
=>
{
fetch
(
'
/counter
'
,
{
method
:
'
GET
'
}).
then
(
resp
=>
{
resp
.
text
().
then
(
text
=>
{
resp
.
text
().
then
(
data
=>
{
document
.
getElementById
(
'
3
'
).
innerHTML
=
text
document
.
getElementById
(
'
3
'
).
innerHTML
=
JSON
.
parse
(
data
).
counter
})
})
})
})
}
}
function
postUsingFetch
(){
function
postUsingFetch
(){
fetch
(
'
/counter
'
,
{
method
:
'
POST
'
})
fetch
(
'
/counter
'
,
{
.
then
(
resp
=>
{
method
:
"
POST
"
,
return
resp
.
text
()
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
body
:
JSON
.
stringify
({
'
add
'
:
1
})
})
})
.
then
(
text
=>
{
.
then
(
resp
=>
resp
.
json
()
)
document
.
getElementById
(
'
4
'
).
innerHTML
=
text
.
then
(
data
=>
{
document
.
getElementById
(
'
4
'
).
innerHTML
=
data
.
counter
})
})
}
}
window
.
addEventListener
(
'
load
'
,
function
()
{
window
.
addEventListener
(
'
load
'
,
()
=>
{
getUsingXMLHttpRequest
()
getUsingXMLHttpRequest
()
postUsingXMLHttpRequest
()
postUsingXMLHttpRequest
()
getUsingFetch
()
getUsingFetch
()
...
...
This diff is collapsed.
Click to expand it.
server.js
+
8
−
3
View file @
885ed4d2
...
@@ -2,18 +2,23 @@ const express = require('express')
...
@@ -2,18 +2,23 @@ const express = require('express')
let
app
=
express
()
let
app
=
express
()
app
.
use
(
express
.
static
(
'
public
'
))
app
.
use
(
express
.
static
(
'
public
'
))
// Parses all incoming messages of type "application/json" (i.e. body of requests will
// appear to be JSON by default)
app
.
use
(
express
.
json
())
let
counter
=
0
;
let
counter
=
0
;
app
.
route
(
'
/counter
'
)
app
.
route
(
'
/counter
'
)
.
get
((
req
,
res
)
=>
{
.
get
((
req
,
res
)
=>
{
res
.
status
(
200
)
res
.
status
(
200
)
res
.
send
(
`Count:
${
counter
}
`
)
res
.
setHeader
(
'
Content-Type
'
,
'
application/json
'
)
res
.
json
({
counter
})
// short form of { 'counter': counter }
})
})
.
post
((
req
,
res
)
=>
{
.
post
((
req
,
res
)
=>
{
counter
+=
req
.
body
[
'
add
'
];
res
.
status
(
300
)
res
.
status
(
300
)
counter
++
res
.
setHeader
(
'
Content-Type
'
,
'
application/json
'
)
res
.
send
(
`Count:
${
counter
}
`
)
res
.
json
({
counter
})
// short form of { 'counter': counter }
})
})
app
.
listen
(
3000
,
()
=>
{
app
.
listen
(
3000
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment