📺 Develpreneur YouTube Episode

Video + transcript

Using Ajax To Call An API and Then Redirect To New Page

2023-08-31 •Youtube

Detailed Notes

This video looks at one piece of our URL shortener solution. We build an html page that will take the parameter, call the API, grab the original URL and then redirect the user to that location.

You can find out more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list and you will periodically receive coupons for courses as well as notifications of the latest releases.

Transcript Text
foreign
[Music]
well hello and welcome back we are in an
overarching project we're walking
through building an application and it
is a URL shortener a little like a bitly
or owl.ly some of those kinds of sites
or those kinds of functionalities and we
built out our API we've built our
database we built a connection now one
of the things we're going to do is we're
going to in this little bit of change
initially we're going to build our web
app that is essentially the redirector
because we're going to give them back a
full URL they're going to give us
something for example this long thing
they're going to get this short thing
and then when they call this short thing
we want it to send them to this long
thing their original URL much like you
see
so in order to do so we're going to use
a we're going to build a Django
application a web application python
we're going to save that for another day
instead because this is so simple we are
actually going to do this in JavaScript
so we're going to look through today our
focus is going to be redirecting a user
within JavaScript and then also making
an API call within JavaScript so it's
going to be a really simple little
script we're going to work on
now I have it somewhere over here
redirect HTML
now let me I went ahead and typed a lot
of this stuff out because I've been
playing around with it quite a bit there
are some things that we'll talk about
separate in a different issue talking
about deploying out our API that I sort
of ran through as I was testing it
now what I do here uh in this I think
everybody probably knows if you're
asking this question you're going to
start with your script tags so this is
straight up HTML document we're going to
throw this on our local Apache server on
a on a Mac
we start off keep it real simple we're
going to start right into the script so
I'm going to set the default value to
where we're going to go
developreneur.com I'm just going to
throw them somewhere then what I'm going
to do is I'm going to grab the URL from
the location itself and we're going to
see this in the console actually
we're gonna do it like this we'll do
the
the URL
called
and we're going to show that off
and then and so this is where the sort
of key is what we're going to do is
we're basically say hey whatever you
send us we're going to peel off a uh
we're gonna peel off a
couple variables in this case in
particular this would be
uh if we go back here
we would assume that we were sitting at
like https
short.srv we want to peel off that
number
now it's gonna be a little different
because the way we're doing it here
we're not going to use a slash we're
going to actually do a we're going to
assume there's a slash we're going to
put a uh quote a question mark in front
though because just how we've set it up
for this specific page you can tweak it
as needed but what we need to do is we
need to grab what is it what did they
send us and we're going to do that
through the window location
and then
this is where this is the meat of what
we're going to talk about is making that
API call so an API call in JavaScript it
provides this thing called xh XML HTTP
request and it is a well well used
commonly used approach to making calls
and so what we're going to do in our
case is we're going to call our API if
you remember we had it set up here at
127's uh well we had it set up
differently but if I go over here
uh do I have
I don't have Postman up but I can do it
over here if I go to 127.90 what is that
90 90
uh where I put that
over here
yeah 90 90. if I do the get
this is a postman window within Visual
Studio code by the way which didn't
mention in the postman thing but if you
want to it's very easy you can go into
uh I believe that's it yeah extensions
and if you go look up Postman you will
see that they have Postman stuff so that
you can actually go in and again one of
those things that's pretty useful for
building out your your API and testing
it Visual Studio code does a lot of that
and this would be as if I was in Postman
so I can see my calls all that good
stuff not to get too far afield from
that so let's if I can click on that
there we go then if we do a get we sent
the short name
uh here it doesn't find it but if we
give it one two three four five six
that's the one we have that does work
then it's going to send us back this URL
so that's what we want to do
here
is this is our HP or I'm sorry our API
call
so we are this would be the API server
and port and then this is the endpoint
shorten and then URL which you can see
here same thing
that's our API endpoint
and so we come over here we're going to
make that call and what we're going to
do is we're just going to tack on this
URL that we have now we could do there
are other ways we could do this but it's
just easier to do it this way so we're
going to tack that in
and actually what we can do
uh well uh
we'll do it like this for now and then
all you have to do so you're going to
say open it
which says hey do I have a is basically
gets a connection to the server and then
send actually sends the message so open
doesn't do anything by itself as far as
like calling the
um calling the API but send does
and then what we're going to need to do
is within that request we want to
basically look for what's going on
so we're going to do is we're going to
come in and we're going to do a console
log the request and then we're just
going to sit around and wait because
this when this comes in we're going to
say hey oh and this is on I'm sorry this
is when the Call Comes in when it
actually gets result back we're going to
say hey when you get the result back
go ahead and just show what the response
is I said well the request is
and then if the status
is equal to 200
I got next one in there then we're going
to parse you do a json.parse so we're
going to turn this thing which is Json
so we're going to parse that thing out
the response is
which you can see over here
see we have that this is that response
we're going to parse this thing out
and that's going to allow us to grab the
data which is the actual URL see data is
the URL
at that and then our new URL is going to
be that and then what we end up doing is
going to redirect and we'll cover that
in just a minute but first let's look at
what this is going to do if we get an
error we get the status text so it's
basically hey if we don't get a 200 then
we're going to kick out an error so we
can take this let me take this HTML and
we can put it out in our local basically
put it on a web server just about
anywhere
and so we're going to do that we've got
running local on this Mac we've got one
and oh by the way I've got I'm sorry
after that I just have this like
this little page and I could do some
more with it but I'm not gonna let's do
this uh
yeah let's do it this way uh we're gonna
do just a paragraph
let's do this which can be really simple
and this is going to be the result let's
do response
and that's going to be
[Music]
um
do this we'll do one of our response
whoops
and
our status
I don't know that we're gonna be able to
do this but well let's see what happens
um
so if we do that if I do that
uh I do status
equals
Quest dot status
and
response
equals
request Dot
status text let's say
um
I will say response
don't know that that'll always give us
something but
uh
let's forget it I don't want to do that
because I don't wanna that's gonna be
too much of a pain
um it's easier to look at the console
log so let's just do that
I don't want to go through set all those
values up
[Music]
okay so
besides that would mess this up now so
over here I've got this and we're going
to look in our console here which I'm
going to have to so
so if I
like so if I come whoop if I come in
here
and I give it
um
let's give it
um
let's just give it one two three four
whoops I have to make sure that it is
HTTP colon
backslash backslash
there we go okay see I don't know if I
can can I pull this out into something a
little bigger yeah okay so here's the
log so the first thing it does it comes
through is it says hey console log oh I
don't have the URL curl um
let's do this
let's
so I'm going to copy redirect index.html
this is just to make it really easy I'm
going to copy that over to my
um my local server
and then
oh wait I want to run it
here x gaming same thing uh one two
three four yeah
so now
look
okay so here in the log we come in the
URL we do what's called here is what was
sent
and then it's going to go in and it
here's a request that went through we
have that here we log the request
and then
we come through here and we didn't get
an error so we got this one so let's
call this
the
data
returned
and if we do that
now let's try it
just because there now it's going to
give a state of return
and you can see it here
didn't find the link because we didn't
give it a useful ID but if we come over
here
and do the same thing oh I did have that
somewhere oh I see
uh now if I do one two three four five
six
then
it's going to give us our actual link
and we will be able to confirm that so
here we call one two three four five six
now it gives us this longer link which
if we click on it
it's going to go open up this page on
our website
so we're almost there so now what we
want to do instead of doing this thing
we want to come in here
and we'll leave the redirecting
hopefully I don't even see it but now
this is a very simple thing in
JavaScript we just want to redirect so
we're going to do a window location
replace with that new URL which is this
thing so now if I call it
oop if I call it with the correct one
oh
sorry
oh and by the way if I call it the wrong
one it's going to do the new URL which
is going to take me just straight to
developmentor.com so let's go look at
those two examples again
copy this over now
and now now if I call it with the right
one
then it is not doing did I forget to
return it let's see the URL equals that
that's equals that
oh let me look I bet it's not
let's do a new private window just to
make sure
it's not doing my redirect
let's see what did I miss
oh I bet it's coming through too quick
so once I get it then I want to do it
like this
that will probably do it for me
um so if I do this that was something I
didn't run into earlier
so now
I go to that guy and run
there we go so now it's coming to that
human based systems if I go back and do
HTTP colon backslash localhost
uh four three two one so it's a wrong
one then
oops
what did I do
okay that one worked why did I not get
it oh
um I see
so if it comes here
there we go
that's it my mistake because then
I need to do
oh yeah I can do it this way
I ran into my little race condition so
now if I do this same thing
uh what I want oh I want to be able to
type in that right
so if I do oh
how did HTTP going backslash backslash a
little
post
four three two one
oh interesting it doesn't like that so
let's see if it's
um
I got a console is it kicking that out
oh no it is trying to get to
somewhere why is it having issues with
that
replace
let's see
oh if it's not that's a problem it's not
the right one so
um
I need that to be
that's what I need is I need to make
sure that that is
a 200 and uh there we go
so if
uh the URL
not equal to I think not found then
we can redirect
oh let's see this
so if it's not linked not found then
it's going to go ahead and reset it
otherwise it's going to go ahead and run
it
um am I in the right place yeah that's
redirect so if I go here if I go over
here
copy this thing out
now if I do it
so if I go H go back slow uh backslash
backslash
where am I at
localhost I spelled it right okay
four through two one
now oh interesting
um
let's see
this would be an interesting thing that
we've run into so if I do http
host four three two one
not found so if I come here
let's do this
console.log
new URL
so it'll be the last thing that we see
so let's go over here
update
come here
oh so now if we do HTTP column backslash
backslash
localhost
uh four three two one let's see what
does it tell us
so it's going to say here
that it was
oh data returned link not found right
okay oh that's right that's the API
so this is link not found
uh why is it
not found
oh
it's not new URL it's if
dad come on that's the problem and
that's
data this is why you have to pay
attention so if a result I've got to
check the result is equal to that
then we go through it and then we do
this okay
uh now I need to take that and copy it
out
and go here and now if we go to that one
now it's going to send us there
since it's on page if we go to localhost
one two three four five six
then we go here and we don't need to do
the redirecting we can do other stuff
with that but
we're going to go ahead so we could just
leave it like this but we'll do that
just because
that's probably a nice thing to do we
could probably put a nice little
advertisement even there or something
like that or even a little link that
says hey if it doesn't work click on
this to let me know
that
is calling an API
doing something with the data as we've
seen we've got and that's based on we're
going to parse that out because it comes
back
in this uh this Json so we're going to
parse it out we're going to pull out the
value that we want
and we're going to do something with it
in this case we redirect them to the new
URL so now we have something that we can
actually place on a server
and when they call that theoretically
they could do it like shortname.com
if they go to that site make that call
with that ID we should be able to peel
that off now we've changed this around a
little bit but this is our starting
point and this is the key is that it's
actually really easy to make calls
within JavaScript now I can do other
stuff I can do something actually far
more complicated and I'll show you one
real quick like if I wanted to do
uh let's do
Source equals blah blah
and then
um
let's do this let's do p
ID equals uh
source
we could come down here
um
uh oh don't want to do that oh
well what we can do we could we can pull
an entire page doing this so if I do the
same thing let me just do this
let me open
uh no because it's going to be a
complicated kind of thing
what you can do is this request.open can
call anything so if I can take this I
could actually call a website and I
could actually display a page within it
because it's whatever we are peeling out
here
it's whatever comes from that response
so we're making the API call but we
could just as easily just as we can make
this API call
through a web page
so if I do the same thing and I make
that call
uh six five four three two one
then I get this back so note I'm getting
Json data back I don't have to actually
Json parse it
I could actually just
try to work with it directly so if I get
HTML like I do with any browser page I
can actually do that so I could do this
for things like
scraping data out of a request and
things of that nature
it goes a little bit beyond what we can
do but I wanted to get to the basics of
making calls from JavaScript it's really
simple you want to use the here that xh
XML HTTP request open it send it and
then unload is where you're going to
actually do some of these that means
that hey once I've got my result back
then I'm going to do something with it
and basically it's from the right here
it's from the response so whatever comes
back
this is the response if it's HTML it'll
be in the response and then you can
wheel and deal with it and do what you
need okay thanks a lot for your time and
I hope you come back next time we will
have some more to go over and let's keep
plotting our way through this thing next
one depends on what your order is so for
now go out there have some fun with your
API and we will catch you next time
thank you
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology our how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.developmentor we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
Transcript Segments
0.42

foreign

18.89

[Music]

27.18

well hello and welcome back we are in an

30.96

overarching project we're walking

32.94

through building an application and it

36.239

is a URL shortener a little like a bitly

39.6

or owl.ly some of those kinds of sites

43.14

or those kinds of functionalities and we

46.44

built out our API we've built our

49.26

database we built a connection now one

51.42

of the things we're going to do is we're

52.8

going to in this little bit of change

55.02

initially we're going to build our web

56.46

app that is essentially the redirector

59.94

because we're going to give them back a

62.219

full URL they're going to give us

63.48

something for example this long thing

66.479

they're going to get this short thing

69.06

and then when they call this short thing

71.04

we want it to send them to this long

74.4

thing their original URL much like you

76.74

see

77.939

so in order to do so we're going to use

79.92

a we're going to build a Django

81.96

application a web application python

83.7

we're going to save that for another day

85.619

instead because this is so simple we are

89.34

actually going to do this in JavaScript

90.9

so we're going to look through today our

94.14

focus is going to be redirecting a user

98.18

within JavaScript and then also making

101.46

an API call within JavaScript so it's

103.56

going to be a really simple little

105

script we're going to work on

107.28

now I have it somewhere over here

109.5

redirect HTML

111.18

now let me I went ahead and typed a lot

113.22

of this stuff out because I've been

114.119

playing around with it quite a bit there

116.759

are some things that we'll talk about

118.799

separate in a different issue talking

122.1

about deploying out our API that I sort

124.38

of ran through as I was testing it

127.619

now what I do here uh in this I think

130.619

everybody probably knows if you're

132.18

asking this question you're going to

134.04

start with your script tags so this is

135.599

straight up HTML document we're going to

137.76

throw this on our local Apache server on

140.099

a on a Mac

141.42

we start off keep it real simple we're

144.18

going to start right into the script so

145.98

I'm going to set the default value to

148.16

where we're going to go

149.959

developreneur.com I'm just going to

151.379

throw them somewhere then what I'm going

153.18

to do is I'm going to grab the URL from

156.9

the location itself and we're going to

158.459

see this in the console actually

160.739

we're gonna do it like this we'll do

165.72

the

167.58

the URL

169.08

called

172.739

and we're going to show that off

175.019

and then and so this is where the sort

177.66

of key is what we're going to do is

178.44

we're basically say hey whatever you

179.459

send us we're going to peel off a uh

182.58

we're gonna peel off a

184.739

couple variables in this case in

187.739

particular this would be

190.379

uh if we go back here

193.56

we would assume that we were sitting at

195.48

like https

196.879

short.srv we want to peel off that

200.099

number

200.94

now it's gonna be a little different

202.08

because the way we're doing it here

203.519

we're not going to use a slash we're

205.86

going to actually do a we're going to

207.36

assume there's a slash we're going to

208.44

put a uh quote a question mark in front

211.379

though because just how we've set it up

213.84

for this specific page you can tweak it

215.959

as needed but what we need to do is we

218.58

need to grab what is it what did they

220.56

send us and we're going to do that

221.879

through the window location

223.86

and then

225.239

this is where this is the meat of what

227.879

we're going to talk about is making that

229.2

API call so an API call in JavaScript it

233.64

provides this thing called xh XML HTTP

236.519

request and it is a well well used

241.2

commonly used approach to making calls

244.98

and so what we're going to do in our

246.18

case is we're going to call our API if

249.12

you remember we had it set up here at

252.12

127's uh well we had it set up

254.459

differently but if I go over here

256.979

uh do I have

259.26

I don't have Postman up but I can do it

261.239

over here if I go to 127.90 what is that

264.36

90 90

266.94

uh where I put that

269.94

over here

271.86

yeah 90 90. if I do the get

279.72

this is a postman window within Visual

284.04

Studio code by the way which didn't

286.44

mention in the postman thing but if you

287.88

want to it's very easy you can go into

290.479

uh I believe that's it yeah extensions

292.979

and if you go look up Postman you will

296.88

see that they have Postman stuff so that

299.58

you can actually go in and again one of

301.919

those things that's pretty useful for

304.08

building out your your API and testing

306.6

it Visual Studio code does a lot of that

308.52

and this would be as if I was in Postman

310.62

so I can see my calls all that good

313.38

stuff not to get too far afield from

316.8

that so let's if I can click on that

318.54

there we go then if we do a get we sent

321.66

the short name

322.979

uh here it doesn't find it but if we

325.68

give it one two three four five six

327.18

that's the one we have that does work

330

then it's going to send us back this URL

332.4

so that's what we want to do

334.38

here

335.699

is this is our HP or I'm sorry our API

339.479

call

341.28

so we are this would be the API server

344.4

and port and then this is the endpoint

346.5

shorten and then URL which you can see

349.5

here same thing

351.66

that's our API endpoint

354.539

and so we come over here we're going to

355.68

make that call and what we're going to

356.699

do is we're just going to tack on this

360.12

URL that we have now we could do there

363

are other ways we could do this but it's

364.44

just easier to do it this way so we're

366.72

going to tack that in

368.28

and actually what we can do

374.419

uh well uh

377.34

we'll do it like this for now and then

379.32

all you have to do so you're going to

380.22

say open it

381.9

which says hey do I have a is basically

384

gets a connection to the server and then

387

send actually sends the message so open

389.52

doesn't do anything by itself as far as

392.039

like calling the

394.319

um calling the API but send does

397.62

and then what we're going to need to do

400.62

is within that request we want to

403.38

basically look for what's going on

405.72

so we're going to do is we're going to

406.74

come in and we're going to do a console

408.72

log the request and then we're just

410.58

going to sit around and wait because

411.96

this when this comes in we're going to

413.4

say hey oh and this is on I'm sorry this

415.259

is when the Call Comes in when it

416.639

actually gets result back we're going to

418.38

say hey when you get the result back

420.3

go ahead and just show what the response

423.3

is I said well the request is

425.28

and then if the status

427.56

is equal to 200

430.38

I got next one in there then we're going

433.44

to parse you do a json.parse so we're

436.38

going to turn this thing which is Json

438.72

so we're going to parse that thing out

440.9

the response is

443.039

which you can see over here

447

see we have that this is that response

448.979

we're going to parse this thing out

453.18

and that's going to allow us to grab the

455.099

data which is the actual URL see data is

458.52

the URL

460.38

at that and then our new URL is going to

463.139

be that and then what we end up doing is

465.18

going to redirect and we'll cover that

466.8

in just a minute but first let's look at

468.72

what this is going to do if we get an

470.22

error we get the status text so it's

472.259

basically hey if we don't get a 200 then

475.38

we're going to kick out an error so we

477.66

can take this let me take this HTML and

481.44

we can put it out in our local basically

483.84

put it on a web server just about

485.88

anywhere

487.44

and so we're going to do that we've got

488.94

running local on this Mac we've got one

492.539

and oh by the way I've got I'm sorry

494.88

after that I just have this like

497.16

this little page and I could do some

499.74

more with it but I'm not gonna let's do

502.979

this uh

505.319

yeah let's do it this way uh we're gonna

508.02

do just a paragraph

510.599

let's do this which can be really simple

514.68

and this is going to be the result let's

517.979

do response

521.339

and that's going to be

523.01

[Music]

523.38

um

527.399

do this we'll do one of our response

532.92

whoops

534.44

and

536.18

our status

543.48

I don't know that we're gonna be able to

544.98

do this but well let's see what happens

547.8

um

549

so if we do that if I do that

552.6

uh I do status

555.899

equals

558

Quest dot status

561

and

563.88

response

566.459

equals

569.04

request Dot

571.5

status text let's say

576.899

um

579.12

I will say response

582.48

don't know that that'll always give us

583.92

something but

588.08

uh

589.62

let's forget it I don't want to do that

591.18

because I don't wanna that's gonna be

592.44

too much of a pain

594.24

um it's easier to look at the console

596.04

log so let's just do that

597.959

I don't want to go through set all those

599.22

values up

600.15

[Music]

601.82

okay so

604.2

besides that would mess this up now so

606.48

over here I've got this and we're going

607.68

to look in our console here which I'm

609.42

going to have to so

611.58

so if I

612.839

like so if I come whoop if I come in

615.3

here

616.2

and I give it

618.36

um

620.36

let's give it

623.88

um

624.779

let's just give it one two three four

629.88

whoops I have to make sure that it is

632.22

HTTP colon

634.2

backslash backslash

637.08

there we go okay see I don't know if I

640.14

can can I pull this out into something a

642.48

little bigger yeah okay so here's the

644.579

log so the first thing it does it comes

646.079

through is it says hey console log oh I

649.32

don't have the URL curl um

651.6

let's do this

657

let's

659.1

so I'm going to copy redirect index.html

662.279

this is just to make it really easy I'm

663.66

going to copy that over to my

666.6

um my local server

669.48

and then

670.92

oh wait I want to run it

673.5

here x gaming same thing uh one two

676.8

three four yeah

679.74

so now

682.74

look

686.339

okay so here in the log we come in the

689.519

URL we do what's called here is what was

691.5

sent

693.24

and then it's going to go in and it

695.64

here's a request that went through we

697.56

have that here we log the request

702.06

and then

703.44

we come through here and we didn't get

705.839

an error so we got this one so let's

710.22

call this

715.079

the

718.5

data

721.019

returned

724.8

and if we do that

729.42

now let's try it

731.399

just because there now it's going to

733.26

give a state of return

736.26

and you can see it here

738.899

didn't find the link because we didn't

740.579

give it a useful ID but if we come over

742.32

here

743.399

and do the same thing oh I did have that

745.86

somewhere oh I see

747.54

uh now if I do one two three four five

749.16

six

750.779

then

752.579

it's going to give us our actual link

758.16

and we will be able to confirm that so

761.16

here we call one two three four five six

762.899

now it gives us this longer link which

765.779

if we click on it

767.7

it's going to go open up this page on

771.3

our website

772.74

so we're almost there so now what we

775.32

want to do instead of doing this thing

777.3

we want to come in here

779.279

and we'll leave the redirecting

780.66

hopefully I don't even see it but now

783.26

this is a very simple thing in

785.399

JavaScript we just want to redirect so

787.139

we're going to do a window location

788.519

replace with that new URL which is this

792.36

thing so now if I call it

795

oop if I call it with the correct one

801.06

oh

802.92

sorry

804.06

oh and by the way if I call it the wrong

805.86

one it's going to do the new URL which

808.38

is going to take me just straight to

809.7

developmentor.com so let's go look at

812.16

those two examples again

814.56

copy this over now

821.42

and now now if I call it with the right

825.72

one

828.66

then it is not doing did I forget to

831.48

return it let's see the URL equals that

834.3

that's equals that

838.44

oh let me look I bet it's not

842.22

let's do a new private window just to

844.86

make sure

851.639

it's not doing my redirect

854.7

let's see what did I miss

862.8

oh I bet it's coming through too quick

864.6

so once I get it then I want to do it

868.68

like this

870.66

that will probably do it for me

875.04

um so if I do this that was something I

876.899

didn't run into earlier

880.98

so now

882.779

I go to that guy and run

888.18

there we go so now it's coming to that

890.88

human based systems if I go back and do

893.519

HTTP colon backslash localhost

896.82

uh four three two one so it's a wrong

899.399

one then

902.399

oops

903.54

what did I do

906.3

okay that one worked why did I not get

908.579

it oh

910.92

um I see

914.519

so if it comes here

918.839

there we go

921.66

that's it my mistake because then

927.06

I need to do

931.74

oh yeah I can do it this way

935.579

I ran into my little race condition so

937.44

now if I do this same thing

942.48

uh what I want oh I want to be able to

944.82

type in that right

946.92

so if I do oh

950.16

how did HTTP going backslash backslash a

953.399

little

954.24

post

957

four three two one

959.88

oh interesting it doesn't like that so

966.839

let's see if it's

968.82

um

974.22

I got a console is it kicking that out

976.38

oh no it is trying to get to

981

somewhere why is it having issues with

984.42

that

986.279

replace

993.779

let's see

997.8

oh if it's not that's a problem it's not

1000.68

the right one so

1002.899

um

1004.459

I need that to be

1009.8

that's what I need is I need to make

1011.3

sure that that is

1014.959

a 200 and uh there we go

1019.04

so if

1021.56

uh the URL

1026.179

not equal to I think not found then

1034.66

we can redirect

1044.54

oh let's see this

1051.14

so if it's not linked not found then

1053.179

it's going to go ahead and reset it

1054.26

otherwise it's going to go ahead and run

1056.179

it

1057.38

um am I in the right place yeah that's

1058.46

redirect so if I go here if I go over

1060.74

here

1062.179

copy this thing out

1064.58

now if I do it

1067.94

so if I go H go back slow uh backslash

1071.419

backslash

1073.1

where am I at

1075.34

localhost I spelled it right okay

1078.919

four through two one

1080.66

now oh interesting

1088.1

um

1090.86

let's see

1094.28

this would be an interesting thing that

1095.78

we've run into so if I do http

1099.98

host four three two one

1104.96

not found so if I come here

1116.12

let's do this

1120.08

console.log

1127.58

new URL

1132.919

so it'll be the last thing that we see

1136.46

so let's go over here

1139.4

update

1142.4

come here

1144.74

oh so now if we do HTTP column backslash

1147.86

backslash

1149.539

localhost

1152.24

uh four three two one let's see what

1154.7

does it tell us

1155.96

so it's going to say here

1159.38

that it was

1165.62

oh data returned link not found right

1170.96

okay oh that's right that's the API

1173.6

so this is link not found

1185.24

uh why is it

1195.5

not found

1199.82

oh

1201.2

it's not new URL it's if

1203.78

dad come on that's the problem and

1206.24

that's

1210.4

data this is why you have to pay

1212.48

attention so if a result I've got to

1214.22

check the result is equal to that

1216.14

then we go through it and then we do

1218.179

this okay

1221.32

uh now I need to take that and copy it

1224.24

out

1228.44

and go here and now if we go to that one

1234.08

now it's going to send us there

1237.14

since it's on page if we go to localhost

1242.78

one two three four five six

1246.62

then we go here and we don't need to do

1248.6

the redirecting we can do other stuff

1250.039

with that but

1251.539

we're going to go ahead so we could just

1253.1

leave it like this but we'll do that

1255.5

just because

1256.7

that's probably a nice thing to do we

1258.679

could probably put a nice little

1259.4

advertisement even there or something

1260.96

like that or even a little link that

1262.34

says hey if it doesn't work click on

1264.44

this to let me know

1266.12

that

1267.44

is calling an API

1270.44

doing something with the data as we've

1273.26

seen we've got and that's based on we're

1275.179

going to parse that out because it comes

1276.62

back

1277.76

in this uh this Json so we're going to

1280.82

parse it out we're going to pull out the

1282.14

value that we want

1283.52

and we're going to do something with it

1285.44

in this case we redirect them to the new

1288.74

URL so now we have something that we can

1290.6

actually place on a server

1292.34

and when they call that theoretically

1294.64

they could do it like shortname.com

1297.98

if they go to that site make that call

1300.26

with that ID we should be able to peel

1302.48

that off now we've changed this around a

1304.7

little bit but this is our starting

1308.12

point and this is the key is that it's

1310.22

actually really easy to make calls

1312.14

within JavaScript now I can do other

1315.2

stuff I can do something actually far

1316.76

more complicated and I'll show you one

1318.44

real quick like if I wanted to do

1321.4

uh let's do

1323.86

Source equals blah blah

1327.4

and then

1329.36

um

1331.76

let's do this let's do p

1336.14

ID equals uh

1339.32

source

1341.659

we could come down here

1345.74

um

1349.24

uh oh don't want to do that oh

1353.9

well what we can do we could we can pull

1357.74

an entire page doing this so if I do the

1360.62

same thing let me just do this

1362.6

let me open

1366.76

uh no because it's going to be a

1368.78

complicated kind of thing

1371.48

what you can do is this request.open can

1374.24

call anything so if I can take this I

1377.179

could actually call a website and I

1379.039

could actually display a page within it

1380.659

because it's whatever we are peeling out

1382.82

here

1383.96

it's whatever comes from that response

1386.419

so we're making the API call but we

1388.7

could just as easily just as we can make

1390.98

this API call

1394.22

through a web page

1396.74

so if I do the same thing and I make

1399.14

that call

1400.46

uh six five four three two one

1404.24

then I get this back so note I'm getting

1407.36

Json data back I don't have to actually

1410.74

Json parse it

1413

I could actually just

1414.799

try to work with it directly so if I get

1417.039

HTML like I do with any browser page I

1420.32

can actually do that so I could do this

1422.059

for things like

1423.62

scraping data out of a request and

1425.9

things of that nature

1427.52

it goes a little bit beyond what we can

1429.08

do but I wanted to get to the basics of

1431.96

making calls from JavaScript it's really

1434.96

simple you want to use the here that xh

1437.96

XML HTTP request open it send it and

1442.1

then unload is where you're going to

1444.38

actually do some of these that means

1445.46

that hey once I've got my result back

1447.02

then I'm going to do something with it

1449.48

and basically it's from the right here

1453.14

it's from the response so whatever comes

1455.059

back

1456.02

this is the response if it's HTML it'll

1458.299

be in the response and then you can

1460.039

wheel and deal with it and do what you

1461.419

need okay thanks a lot for your time and

1464.12

I hope you come back next time we will

1465.74

have some more to go over and let's keep

1468.02

plotting our way through this thing next

1469.82

one depends on what your order is so for

1472.28

now go out there have some fun with your

1474.38

API and we will catch you next time

1487.059

thank you

1493.88

hello this is Rob with developmentor

1496.28

also known as building better developers

1498.38

wanted to announce that we have

1500.74

school.developmentor.com feel free to

1503.059

check it out if you like any of this

1505.22

information any of the content that

1507.14

we've sent and you would like to see

1508.34

more you can come out you can enroll for

1510.44

free we have free courses we've got

1512.72

places for you to get better at just

1515.36

learning a technology our how to's you

1518

can work on your business skills we can

1520.1

help you with becoming a better

1521.539

developer as encoding and things like

1523.94

that a lot of the stuff you've seen on

1525.62

YouTube we also have out at

1527.98

school.developmentor we just have it a

1530.059

little more of a educational format and

1532.58

a way for you to track your progress as

1535.1

you move forward becoming a better

1537.26

developer