JQuery 和 Ajax:无法读取未定义的属性“长度”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14199949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery and Ajax: Cannot read property 'length' of undefined
提问by qqruza
I am trying to get a particular request from website by using variable in the URL and AJAX to display JSON. It seems like everything is working but there is an error "TypeError: Cannot read property 'length' of undefined". A debugger points to JQuery file and the line in my script ($.ajax.success).
我试图通过使用 URL 和 AJAX 中的变量来显示 JSON 来从网站获取特定请求。似乎一切正常,但出现错误“类型错误:无法读取未定义的属性‘长度’”。调试器指向 JQuery 文件和脚本中的行 ($.ajax.success)。
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
$(document).ready(function () {
var output = $('#news');
var postid = getQueryVariable('id');
$.ajax({
url: 'http://www.domain.pro/api/get_post/?post_id=' + postid + '&post_type=news',
async: false,
callback: 'callback',
crossDomain: true,
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'jsonp',
timeout: 5000,
success: function (data, status) {
$.each(data.posts, function (i, item) {
var news = '<div>' + item.title + '</div><div>' + item.content + '</div><hr/>';
output.append(news);
});
},
error: function () {
output.text('There was an error loading the data.');
}
});
})
Could you please help me to sort this out? Really appreciate your halp.
你能帮我解决这个问题吗?真的很感谢你的帮助。
采纳答案by TNCodeMonkey
You should validate your data before performing operations on it:
您应该在对其执行操作之前验证您的数据:
UPDATED
更新
$.ajax({
url: 'http://www.domain.pro/api/get_post/?post_id=' + postid + '&post_type=news',
async: false,
callback: 'callback',
crossDomain: true,
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'jsonp',
timeout: 5000,
success: function (data, status) {
if(data != undefined && data.post != undefined){
$('#news').append('<div>' + data.post.title + '</div><div>' + data.post.content + '</div><hr/>');
}
},
error: function () {
output.html('<h1 class="error">There was an error loading the data.</h2>');
}
});
回答by Rahul
you applying $.each on data.post which could be undefined if your data is null. $.each expects and array or collection that is why you getting an error for length. since your data is null hence data.post would be undefined. you could check before applying $.each like
您在 data.post 上应用 $.each ,如果您的数据为空,则可能未定义。$.each 期望和数组或集合,这就是为什么您收到长度错误的原因。由于您的数据为空,因此 data.post 将是未定义的。你可以在应用 $.each 之前检查一下
if(typeof data == "object" && data.post)
//Your code here.
this check should be applied whenever you expect some data from ajax.
每当您希望从 ajax 获取一些数据时,就应该应用此检查。