[英]How to include a variable in a function - Javascript
Ok, so I have a variable that contains some information that needs to go in a function. 好的,所以我有一个包含一些需要进入函数的信息的变量。 For example, if I have;
例如,如果我有;
var apikey="123456789";
function foo() {
$.getJSON('http://api.websiteexample.com/ + apikey');
}
I'm looking to add some information that is stored in a variable to the end of a website address when requestion JSON data. 我想在请求JSON数据时将一些存储在变量中的信息添加到网站地址的末尾。
Is there way I've done in the example correct? 有没有办法在示例中做到正确?
Thanks! 谢谢!
Try like below, 试试如下,
//Note the quotes moved -----------------v
$.getJSON('http://api.websiteexample.com/' + apikey);
When you have it inside quotes.. It would be considered as a string
. 如果你在引号内有它..它将被视为一个
string
。 You need to put it outside quotes for it to know it is an variable. 你需要把它放在引号之外才能知道它是一个变量。
Try it this way 试试这种方式
var apikey="123456789";
function foo(key) {
$.getJSON('http://api.websiteexample.com/ ' + key);
}
foo(apikey);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.