如果有参数,功能不起作用

时间:2018-04-29 09:34:10

标签: javascript html function

在请求json数据后,我通过这样的函数运行它



request.onload = function() {
  var mainData = request.response;
  loadNavbar(mainData);
}


function loadNavbar(jsonObj) {
	length = jsonObj.catagories.length;

	for (var i = 0 ; i < length; i++) {
	list = document.createElement("li");
	span = document.createElement("span");

	itemName = mainData.catagories[i].catName;

	span.innerHTML = itemName;

	list.appendChild(span);
	nav.appendChild(list);

	console.log(i)
	}
}
&#13;
&#13;
&#13;

网页返回错误如下:

main.js:21 Uncaught TypeError: Cannot read property 'length' of undefined
    at loadNavbar (main.js:21)
    at XMLHttpRequest.request.onload (main.js:12)

但是在我改变之后:

&#13;
&#13;
request.onload = function() {
  var mainData = request.response;
  loadNavbar();
}


function loadNavbar() {
	length = mainData.catagories.length;

	for (var i = 0 ; i < length; i++) {
	list = document.createElement("li");
	span = document.createElement("span");

	itemName = mainData.catagories[i].catName;

	span.innerHTML = itemName;

	list.appendChild(span);
	nav.appendChild(list);

	console.log(i)
	}
}
&#13;
&#13;
&#13;

它相应地工作。 我能在第一个片段中知道我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

我非常确定您传递给该函数的对象不包含名为public ActionResult Reports(string ReportType) { MyApplication_dbEntities Entities = new MyApplication_dbEntities (); DataSet1 DataSet1 = new DataSet1(); DataTable ordersTable = DataSet1.Tables["Orders"]; LocalReport localreport = new LocalReport(); localreport.ReportPath = Server.MapPath("~/Reports/Report1.rdlc"); var entities_data = from c in Entities.Orders select c.FirstName; localreport.DataSources.Add(new ReportDataSource("DataSet1", DataSet1.Tables["Orders"])); string reportType = ReportType; string mimeType; string encoding; string fileNameExtension; if (reportType == "PDF") { fileNameExtension = "pdf"; } else { fileNameExtension = "jpg"; } string[] streams; Warning[] warnings; byte[] renderedByte; renderedByte = localreport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings); Response.AddHeader("content-disposition", "attachment:filename + reservaties_report." + fileNameExtension); return File(renderedByte, fileNameExtension); } } 的数组。我建议你有一个拼写错误,它应该被称为catagories

但是,更多信息可能有用。例如,categories对象的内容。