SB1990
SB1990

Reputation: 43

How to pass a javascript variable in an ajax url?

I have a variable as per below:

var myip;

And I want to insert it in the url below:

$.ajax('http://api.ipstack.com/**[myip]**?access_key=mykey')

If I manually put in my ip in place of [myip] it gives me the desired results but I want to automate it.

I tried with the methods given in the url below but it didn't help.

How to pass Javascript variables inside a URL? AJAX

Thanks in advance for going through and helping!

Upvotes: 0

Views: 67

Answers (1)

Code Maniac
Code Maniac

Reputation: 37755

Use string template.

$.ajax(`http://api.ipstack.com/${myip}?access_key=mykey`)
       ^                       ^^^^^^^                 ^

Or using string concatenation.

   $.ajax('http://api.ipstack.com/' + myip + '?access_key=mykey')

Upvotes: 1

Related Questions