我有这段代码:
var browserName = "chrome";
function getBrowserHack() {
var browsersStack = {
"chrome" : {
"hacks": {
"global" : "!!window.chrome;",
"version" : {
"32" : "!!window.chrome && !!window.chrome.webstore;"
}
}
},
"firefox" : {
"hacks": {
"global" : "!!window.sidebar;",
"version" : {
"2": "(function x(){})[-6]=='x';"
}
}
},
"safari" : {
"hacks" : {
"global" : "/constructor/i.test(window.HTMLElement);"
}
},
"opera" : {
"hacks" : {
"global" : "window.opera && window.opera.version() == X;"
}
}
}
return $.getJSON(browsersStack, function(data){
var browsers = data[browserName];
return browsers;
});
}
我需要使用此JSON数据返回函数,并且我有一个错误[object%20Object] 404 (Not Found)
。我做错了什么?
答案 0 :(得分:2)
我认为你误解了getJSON
,你的代码中已经有JS对象了,试试这个:
function getBrowserHack(browserName) {
var browsersStack = {
"chrome" : {
"hacks": {
"global" : "!!window.chrome;",
"version" : {
"32" : "!!window.chrome && !!window.chrome.webstore;"
}
}
},
"firefox" : {
"hacks": {
"global" : "!!window.sidebar;",
"version" : {
"2": "(function x(){})[-6]=='x';"
}
}
},
"safari" : {
"hacks" : {
"global" : "/constructor/i.test(window.HTMLElement);"
}
},
"opera" : {
"hacks" : {
"global" : "window.opera && window.opera.version() == X;"
}
}
}
return browsersStack[browserName];
}
// Now get the browserhack by calling:
var browserHack = getBrowserHack('chrome');
答案 1 :(得分:0)
getJSON
是一个Ajax函数。它向URL发出HTTP请求(第一个参数),并使用第二个参数中使用的函数处理响应。
你甚至没有任何JSON。你只有一个物体。
// Remove this line
return $.getJSON(browsersStack, function(data){
// Keep these two lines
// but use the correct variable name here - you aren't copying the object to data
var browsers = browsersStack[browserName];
return browsers;
// Remove this line
});