简体   繁体   中英

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!

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM