我不断收到错误消息“未捕获的ReferenceError:未定义dropdown_fitness_json。”我认为,如果它不在ready函数之外,那么它将可以访问。
我已经验证我正在访问json数据,但是我一直在出错。我有没有得到的东西,还是只是我没有注意到的语法错误? TIA!
HTML:
<div class="asynchronous_option">
<select id="plan_name" onchange = "dropdown_fitness_json()">
<option value="0">View Plan By Name</option>
</select>
</div><br>
<div id="plan_search_results">In results</div>
JS:
// ASYNCHRONOUS FITNESS PLAN
function dropdown_fitness_json()
{
var fitness_plan_file = "./../json/fitness_plans.json";
//get data back request
var http_request = new XMLHttpRequest();
try
{
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
}
catch (e)
{
// IE Browsers
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// alert something went wrong
alert("Something is wrong with your browser. Please refresh your page and try again.");
return false;
}
}
}
// anon function
http_request.onreadystatechange = function ()
{
if (http_request.readyState === 4)
{
// JS function JSON.parse parses JSON data
var jsonObj = JSON.parse(http_request.responseText);
// jsonObj variable now contains the data structure
var fitness_plans_names = "";
var plan_search_results = "";
for (var i = 0; i < jsonObj.fitness_plans.length; i++)
{
if (jsonObj.names[i].id === document.getElementById("plan_name").value
&& document.getElementById("plan_name").value !== 0)
{
//fitness_plans_names = fitness_plans_names
// + jsonObj.fitness_plans[i].name + "<br>";
plan_search_results = plan_search_results +
jsonObj.fitness_plans[i].name + "<br>" +
jsonObj.fitness_plans[i].price + "<br>" +
jsonObj.fitness_plans[i].meal_type + "<br>" +
jsonObj.fitness_plans[i].duration + "<br>" +
jsonObj.fitness_plans[i].desc + "<br>";
// display individual plan info
// document.getElementById("plan_name").innerHTML = fitness_plans_names;
document.getElementById("plan_search_results").innerHTML = plan_search_results;
}
//document.getElementById("").innerHTML = plan_search_results;
}
}
};
//get data from json file asynchronously to populate dropdown lists
http_request.open("GET", fitness_plan_file, true);
http_request.send();
}
// end return search result function