Javascript 如何在json url中插入变量?

Javascript 如何在json url中插入变量?,javascript,jquery,json,Javascript,Jquery,Json,我试图在json url中使用我定义的变量: var articleName = "test"; $.getJSON( "https://www.googleapis.com/customsearch/v1?key=API_MY&cx=CX_MY&q='+articleName+'&searchType=image&fileType=jpg&imgSize=xlarge&alt=json`", 我试过:q='+articleName+'但它错了

我试图在json url中使用我定义的变量:

var articleName = "test";

$.getJSON( "https://www.googleapis.com/customsearch/v1?key=API_MY&cx=CX_MY&q='+articleName+'&searchType=image&fileType=jpg&imgSize=xlarge&alt=json`",
我试过:
q='+articleName+'
但它错了

那怎么办

var params = {
 key1: val1,
 key2: val2
};

$.getJSON( "https://www.googleapis.com/customsearch/v1", params, function(res) {

});
尝试:

“一些”+yourVar+“文本”

'some'+yourVar+'text'

或者使用模板文本()

$.getJSON(“https://www.googleapis.com/customsearch/v1?key=API_MY&cx=CX_MY&q=“+articleName+”&searchType=image&fileType=jpg&imgSize=xlarge&alt=json
”`

只需在
+
前后使用
,就像
&q='“+articleName+”&

另一种解决方案是使用
数据

选项1:仅用于文章名称

$.getJSON("https://www.googleapis.com/customsearch/v1?key=API_MY&cx=CX_MY&searchType=image&fileType=jpg&imgSize=xlarge&alt=json",{
    "q":articleName
},
选项2:所有参数

$.getJSON("https://www.googleapis.com/customsearch/v1",{
    "q"          : articleName,
    "alt"        : "json",
    "imgSize"    : "xlarge",
    "fileType"   : "jpg",
    "searchType" : "image",
    "cx"         : "CX_MY",
    "key"        : "API_MY"
},

您需要使用匹配的引号。字符串文字只是字符串。它们不会被解析为代码。@SLaks我尝试了“+articleName+”,只需在
+
之前和之后使用
+
,就像
&q='“+articleName+”“&
我非常感谢大家的回答,这是一个简单的字符串连接问题。请尝试再调试一段时间,这肯定会有帮助。