我已经在网上阅读过有关此类错误的信息,并确定它并非来自多个jQuery核心源。我使用的是jQuery-1.10.2.min.js。
我正在处理的页面允许用户从列表中选择一个事件,之后应该在页面上显示属于该事件的活动列表。
jQuery / Ajax代码读取:
$("#event").change(function ()
{
//get the selected value
var eventId = $(this).val();
alert( "sel val is " + eventId);//this works
$.ajax({
dataType: 'json',
type: 'GET',
url: 'stcg-json-responses.php?fct=getJSONAllActivitiesAtEvent',
data: "eventId="+eventId,
cache: false,
success: function(data)
{
alert("Data sent! " + eventId);//this works
$.isArray(data.response)
{
alert("this is an array");//this works
$.each(data.response, function (index, value)
{
// Loop through your results in jQuery and modify your HTML elements
htmlElement.append("<option id='key'>" + value + "</option>");
});
}
},
error: function(xhr, status, error)
{
alert("Data not sent!");
//console.log("Data not sent!");
}
});
使用各种浏览器开发工具查看结果,响应总是很好 - 它是以JSON对象的匿名数组的形式,每个对象以逗号分隔。但上面的脚本崩溃了: $ .each(data.response,function(index,value)
带有Uncaught TypeError。
为了让您更好地了解,响应数据的格式为(例如):
[
{"activity_id":44,"activity_name":"Unknown activity","activity_desc":"Unknown activity","activity_short_code":"U","event_id":4,"capacity":0,"day_of_week":6,"date":"2012-03-24","project_leader":"","open":0},
{"activity_id":87,"activity_name":"Cleanup at Zoo Bellevue","activity_desc":"Zoo et parc d'accueil Bellevue","activity_short_code":"","event_id":4,"capacity":5,"day_of_week":6,"date":"2012-03-24","project_leader":"","open":0},
{"activity_id":89,"activity_name":"Ali Baba cleanup","activity_desc":"Ali Baba cleanup","activity_short_code":"","event_id":4,"capacity":10,"day_of_week":6,"date":"2012-03-24","project_leader":"","open":0}
]
任何人都知道这里发生了什么? 非常感谢, swissphp
P.S。尝试使用普通的Javascript for循环,但它在array.length上崩溃。
=============================================== ===================================== 解决方案 - 基于此处收到的帮助,这是我的新代码,有效:
$("#event").change(function ()
{
//get the selected value
var eventId = $(this).val();
var selectElement = $("#activity");
$.ajax({
dataType: 'json',
type: 'GET',
url: 'stcg-json-responses.php?fct=getJSONAllActivitiesAtEvent',
data: "eventId="+eventId,
cache: false,
success: function(data)
{
if ($.isArray(data))
{
$.each(data, function (index, value)
{
// Loop through your results in jQuery and modify your HTML elements
selectElement.append($('<option></option>').val(index).html(value['activity_name']));
});
}
},
error: function(xhr, status, error)
{ //etc etc etc
非常感谢所有贡献者!
答案 0 :(得分:4)
您试图在$.each()
上致电undefined
,这会导致该错误。来自jQuery source:
each: function( obj, callback, args ) {
var value,
i = 0,
length = obj.length,
isArray = isArraylike( obj );
或者,换句话说,data.response
未在服务器返回的JSON中定义。我怀疑问题在于你错过了if
声明。
$.isArray(data.response)
{
alert("this is an array");//this works
$.each(data.response, function (index, value)
{
// Loop through your results in jQuery and modify your HTML elements
htmlElement.append("<option id='key'>" + value + "</option>");
});
}
这是一个毫无意义的声明($.isArray(data.response)
),后跟无条件的代码块。你可能意味着:
if($.isArray(data.response))
{
alert("this is an array");//this works
$.each(data.response, function (index, value)
{
// Loop through your results in jQuery and modify your HTML elements
htmlElement.append("<option id='key'>" + value + "</option>");
});
}