我从这里得到TypeError: field[i] is undefined
:
function ObserverFetch() {
$.ajax({
type: "POST",
url: "Observer.aspx/ObserverFetch",
data: JSON.stringify({ id: "" }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data, status) {
$.each(data, function (i, field) {
alert(field[i].BidderName);
//$('#dvBids').text(field);
//$("#gvDetails").append("<tr><td>" + field.SrNo + "</td><td>" + field.BidderName + "</td><td>" + field.BidAmt + "</td></tr>");
});
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
}
这是我在field
[{"SrNo":4,"BidderName":"faisal","BidAmt":6000000.0000,"BidDate":"\/Date(1430199508063)\/"},{"SrNo":3,"BidderName":"arbaaz jalil","BidAmt":5000010.0000,"BidDate":"\/Date(1430199494083)\/"},{"SrNo":2,"BidderName":"arbaaz","BidAmt":500000.0000,"BidDate":"\/Date(1430199483530)\/"},{"SrNo":1,"BidderName":"shekhar1","BidAmt":5000.0000,"BidDate":"\/Date(1430199394957)\/"}]
$( '#dvBids')文本(JSON.stringify(数据))。给了我:
{"d":"[{\"SrNo\":4,\"BidderName\":\"faisal\",\"BidAmt\":6000000.0000,\"BidDate\":\"\\/Date(1430199508063)\\/\"},{\"SrNo\":3,\"BidderName\":\"arbaaz jalil\",\"BidAmt\":5000010.0000,\"BidDate\":\"\\/Date(1430199494083)\\/\"},{\"SrNo\":2,\"BidderName\":\"arbaaz\",\"BidAmt\":500000.0000,\"BidDate\":\"\\/Date(1430199483530)\\/\"},{\"SrNo\":1,\"BidderName\":\"shekhar1\",\"BidAmt\":5000.0000,\"BidDate\":\"\\/Date(1430199394957)\\/\"}]"}
$。each(data.d,function(i,field){给我:
TypeError: invalid 'in' operand a
答案 0 :(得分:5)
奇怪的是,服务器的响应是一个JSON对象,它有一个属性d
,它是一个字符串,包含一个数组的JSON。如果是生成该响应的服务器方法,则可能需要对其进行调整,以使data.d
为数组,而不是字符串。
要获得这些结果并循环显示它们,您需要解析它,然后直接使用field
而不是i
:
var fields = JSON.parse(data.d);
$.each(fields, function(i, field) {
alert(field.BidderName);
});
或当然:
$.each(JSON.parse(data.d), function(i, field) {
alert(field.BidderName);
});
旁注:由于data.d
的解析版本是一个数组,因此在任何现代浏览器上,您都可以使用Array#forEach
而不是$.each
,它的参数列表稍微不那么容易混淆回调。 (Array#forEach
可以在IE8及更早版本中轻松填充。)
JSON.parse(data.d).forEach(function(field) {
alert(field.BidderName);
});
两者的实例:
var data = {
"d": "[{\"SrNo\":4,\"BidderName\":\"faisal\",\"BidAmt\":6000000.0000,\"BidDate\":\"\\/Date(1430199508063)\\/\"},{\"SrNo\":3,\"BidderName\":\"arbaaz jalil\",\"BidAmt\":5000010.0000,\"BidDate\":\"\\/Date(1430199494083)\\/\"},{\"SrNo\":2,\"BidderName\":\"arbaaz\",\"BidAmt\":500000.0000,\"BidDate\":\"\\/Date(1430199483530)\\/\"},{\"SrNo\":1,\"BidderName\":\"shekhar1\",\"BidAmt\":5000.0000,\"BidDate\":\"\\/Date(1430199394957)\\/\"}]"
};
snippet.log("Using $.each:");
$.each(JSON.parse(data.d), function(i, field) {
snippet.log(field.BidderName);
});
snippet.log("----");
snippet.log("Using forEach");
JSON.parse(data.d).forEach(function(field) {
snippet.log(field.BidderName);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
答案 1 :(得分:-1)
问题是在overflow:auto
中你已经拥有了数组的每个元素。解决方案是:
var fiel