Javascript 未捕获类型错误:无法读取属性';长度';未定义的
我正在使用jquery移动脚本从我的wordpress站点获取json,但它没有显示任何内容,并返回一个:“uncaughtTypeError:cannotreadproperty'length'of undefined”错误。我知道我在解析json时遇到了麻烦,但我碰巧是一个javascript noob。对于那个既帮我修理又给我解释的人,我感谢你 HTML: 列表视图Javascript 未捕获类型错误:无法读取属性';长度';未定义的,javascript,json,wordpress,jquery-mobile,cordova,Javascript,Json,Wordpress,Jquery Mobile,Cordova,我正在使用jquery移动脚本从我的wordpress站点获取json,但它没有显示任何内容,并返回一个:“uncaughtTypeError:cannotreadproperty'length'of undefined”错误。我知道我在解析json时遇到了麻烦,但我碰巧是一个javascript noob。对于那个既帮我修理又给我解释的人,我感谢你 HTML: 列表视图 页面浏览 <div data-role="page" id="headline"> <div da
页面浏览
<div data-role="page" id="headline">
<div data-role="content">
<ul data-role="listview" id="post-data" data-theme="a"></ul>
</div>
</div>
JavaScript:
$(document).on('pageinit', '#home', function () {
$.ajax({
url: 'http://chris.floppytron.com/api/get_recent_posts/',
dataType: "jsonp",
success: function (result) {
ajax.parseJSONP(result);
},
error: function (request, error) {
alert('Network error has occurred please try again!');
}
});
});
$(document).on('pagebeforeshow', '#headline', function () {
$('#post-data').empty();
$.each(postInfo.result, function (i, row) {
if (row.id == postInfo.id) {
$('#post-data').append('<li>' + row.title + '</li>');
$('#post-data').append('<li>' + row.date + '</li>');
$('#post-data').append('<li>' + row.categories + '</li><br />');
$('#post-data').append('<li>' + row.content + '</li>');
$('#post-data').listview('refresh');
}
});
});
$(document).on('vclick', '#post-list li a', function () {
postInfo.id = $(this).attr('data-id');
$.mobile.changePage("#headline", {
transition: "slide",
changeHash: false
});
});
var postInfo = {
id: null,
result: null
}
var ajax = {
parseJSONP: function (result) {
postInfo.result = result.results;
$.each(result.results, function (i, row) {
console.log(JSON.stringify(row));
$('#post-list').append('<li><a href="" data-id="' + row.id + '"><img src="' + row.thumbnail + '"/><h3>' + row.title + '</h3><p>' + row.categories + '</p><br /><p>' + row.date + '</p></a></li>');
});
$('#post-list').listview('refresh');
}
}
$(document).on('pageinit','#home',函数(){
$.ajax({
网址:'http://chris.floppytron.com/api/get_recent_posts/',
数据类型:“jsonp”,
成功:功能(结果){
parseJSONP(结果);
},
错误:函数(请求、错误){
警报('发生网络错误,请重试!');
}
});
});
$(文档).on('pagebeforeshow','#headline',函数(){
$(“#发布数据”).empty();
$.each(postInfo.result,函数(i,行){
if(row.id==postInfo.id){
$(“#发布数据”).append(“”+row.title+” ”);
$(“#发布数据”).append(“”+row.date+” ”);
$(“#发布数据”).append(“”+row.categories+”
);
$(“#发布数据”).append(“”+row.content+” );
$(“#发布数据”).listview(“刷新”);
}
});
});
$(文档).on('vclick','#post list li a',函数(){
postInfo.id=$(this.attr('data-id');
$.mobile.changePage(#标题){
过渡:“幻灯片”,
changeHash:false
});
});
var postInfo={
id:null,
结果:空
}
var ajax={
parseJSONP:函数(结果){
postInfo.result=result.results;
$.each(result.results,函数(i,行){
log(JSON.stringify(row));
$(“#帖子列表”)。追加(“”);
});
$(“#发布列表”).listview(“刷新”);
}
}
$.parseJSONP
它是$.parseJSON
没有“p”。无论如何,您不需要解析返回的Ajax结果,因为它已经准备好填充了.results
属性,它是.posts
postInfo.result = result.posts;
$.each(postInfo.result, function (i, row) {
$(document).on('pagecreate', '#home', function () {
$.ajax({
url: 'http://chris.floppytron.com/api/get_recent_posts/',
dataType: "jsonp",
success: function (result) {
addRows(result);
},
error: function (request, error) {
alert('Network error has occurred please try again!');
}
});
});
$(document).on('pagebeforeshow', '#headline', function () {
$('#post-data').empty();
$.each(postInfo.result, function (i, row) {
if (row.id == postInfo.id) {
$('#post-data').append('<li>' + row.title + '</li>');
$('#post-data').append('<li>' + row.date + '</li>');
$('#post-data').append('<li>' + row.categories + '</li><br />');
$('#post-data').append('<li>' + row.content + '</li>');
$('#post-data').listview('refresh');
}
});
});
$(document).on('click', '#post-list li a', function () {
postInfo.id = $(this).attr('data-id');
$.mobile.changePage("#headline", {
transition: "slide",
changeHash: false
});
});
var postInfo = {
id: null,
result: null
};
function addRows(result) {
postInfo.result = result.posts;
$.each(postInfo.result, function (i, row) {
$('#post-list').append('<li><a href="" data-id="' + row.id + '"><img src="' + row.thumbnail + '"/><h3>' + row.title + '</h3><p>' + row.categories + '</p><br /><p>' + row.date + '</p></a></li>');
});
$('#post-list').listview('refresh');
};
$(文档).on('pagecreate','#home',函数(){
$.ajax({
网址:'http://chris.floppytron.com/api/get_recent_posts/',
数据类型:“jsonp”,
成功:功能(结果){
添加行(结果);
},
错误:函数(请求、错误){
警报('发生网络错误,请重试!');
}
});
});
$(文档).on('pagebeforeshow','#headline',函数(){
$(“#发布数据”).empty();
$.each(postInfo.result,函数(i,行){
if(row.id==postInfo.id){
$(“#发布数据”).append(“”+row.title+” ”);
$(“#发布数据”).append(“”+row.date+” ”);
$(“#发布数据”).append(“”+row.categories+”
);
$(“#发布数据”).append(“”+row.content+” );
$(“#发布数据”).listview(“刷新”);
}
});
});
$(文档)。在('click','#post list li a',函数(){
postInfo.id=$(this.attr('data-id');
$.mobile.changePage(#标题){
过渡:“幻灯片”,
changeHash:false
});
});
var postInfo={
id:null,
结果:空
};
函数addRows(结果){
postInfo.result=result.posts;
$.each(postInfo.result,函数(i,行){
$(“#帖子列表”)。追加(“”);
});
$(“#发布列表”).listview(“刷新”);
};
你知道它在哪里爆炸了吗?你能检查一下
postInfo.result
的值吗?如果你把undefined作为第一个参数传递给$。each()
你就可以得到上面提到的错误。还有result.results
$(document).on('pagecreate', '#home', function () {
$.ajax({
url: 'http://chris.floppytron.com/api/get_recent_posts/',
dataType: "jsonp",
success: function (result) {
addRows(result);
},
error: function (request, error) {
alert('Network error has occurred please try again!');
}
});
});
$(document).on('pagebeforeshow', '#headline', function () {
$('#post-data').empty();
$.each(postInfo.result, function (i, row) {
if (row.id == postInfo.id) {
$('#post-data').append('<li>' + row.title + '</li>');
$('#post-data').append('<li>' + row.date + '</li>');
$('#post-data').append('<li>' + row.categories + '</li><br />');
$('#post-data').append('<li>' + row.content + '</li>');
$('#post-data').listview('refresh');
}
});
});
$(document).on('click', '#post-list li a', function () {
postInfo.id = $(this).attr('data-id');
$.mobile.changePage("#headline", {
transition: "slide",
changeHash: false
});
});
var postInfo = {
id: null,
result: null
};
function addRows(result) {
postInfo.result = result.posts;
$.each(postInfo.result, function (i, row) {
$('#post-list').append('<li><a href="" data-id="' + row.id + '"><img src="' + row.thumbnail + '"/><h3>' + row.title + '</h3><p>' + row.categories + '</p><br /><p>' + row.date + '</p></a></li>');
});
$('#post-list').listview('refresh');
};