I am getting TypeError: field[i] is undefined
from this:
我得到TypeError:field [i]未定义:
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);
}
});
}
This is what I get in 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').text(JSON.stringify(data)); gives me :
$( '#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) { gives me :
$ .each(data.d,function(i,field){给我:
TypeError: invalid 'in' operand a
2 个解决方案
#1
5
Bizarrely, the response from your server is a JSON object with a single property, d
, which is a string containing the JSON for an array. If it's your server method generating that response, you probably want to adjust it so that data.d
is an array, not a string.
奇怪的是,服务器的响应是一个带有单个属性d的JSON对象,它是一个包含数组JSON的字符串。如果是生成该响应的服务器方法,则可能需要调整它以使data.d是一个数组,而不是字符串。
To get those results and loop through them, you'll need to parse it, and then use field
directly rather than via i
:
要获得这些结果并循环遍历它们,您需要解析它,然后直接使用字段而不是通过i:
var fields = JSON.parse(data.d);
$.each(fields, function(i, field) {
alert(field.BidderName);
});
or of course:
当然还是:
$.each(JSON.parse(data.d), function(i, field) {
alert(field.BidderName);
});
Side note: As the parsed version of data.d
is an array, on any modern browser you can use Array#forEach
rather than $.each
, which has a slightly less-confusing argument list for the callback. (Array#forEach
can be easily polyfilled on IE8 and earlier.)
旁注:由于data.d的解析版本是一个数组,因此在任何现代浏览器中,您都可以使用Array#forEach而不是$ .each,它有一个稍微不那么令人困惑的回调参数列表。 (Array#forEach可以在IE8及更早版本中轻松填充。)
JSON.parse(data.d).forEach(function(field) {
alert(field.BidderName);
});
Live Example of both:
两个实例:
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>
#2
-1
The problem is that in var fiel
you already have a each element of the array. The solutiobn is:
问题是在var fiel中你已经拥有了数组的每个元素。解决方案是:
$.each( obj, function (i, field) {
alert(field.BidderName );
});
#1
5
Bizarrely, the response from your server is a JSON object with a single property, d
, which is a string containing the JSON for an array. If it's your server method generating that response, you probably want to adjust it so that data.d
is an array, not a string.
奇怪的是,服务器的响应是一个带有单个属性d的JSON对象,它是一个包含数组JSON的字符串。如果是生成该响应的服务器方法,则可能需要调整它以使data.d是一个数组,而不是字符串。
To get those results and loop through them, you'll need to parse it, and then use field
directly rather than via i
:
要获得这些结果并循环遍历它们,您需要解析它,然后直接使用字段而不是通过i:
var fields = JSON.parse(data.d);
$.each(fields, function(i, field) {
alert(field.BidderName);
});
or of course:
当然还是:
$.each(JSON.parse(data.d), function(i, field) {
alert(field.BidderName);
});
Side note: As the parsed version of data.d
is an array, on any modern browser you can use Array#forEach
rather than $.each
, which has a slightly less-confusing argument list for the callback. (Array#forEach
can be easily polyfilled on IE8 and earlier.)
旁注:由于data.d的解析版本是一个数组,因此在任何现代浏览器中,您都可以使用Array#forEach而不是$ .each,它有一个稍微不那么令人困惑的回调参数列表。 (Array#forEach可以在IE8及更早版本中轻松填充。)
JSON.parse(data.d).forEach(function(field) {
alert(field.BidderName);
});
Live Example of both:
两个实例:
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>
#2
-1
The problem is that in var fiel
you already have a each element of the array. The solutiobn is:
问题是在var fiel中你已经拥有了数组的每个元素。解决方案是:
$.each( obj, function (i, field) {
alert(field.BidderName );
});