rob.m rob.m - 24 days ago 7
Javascript Question

How to insert a variable inside a json url?

I am trying to use my defined var inside the 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`",


I tried:
q='+articleName+'
but it's wrong

Answer

Just use " before and after +, like &q='"+articleName+"'&

Another solution is using data

Option 1: Just for article name

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

Option 2: All param

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