[英]unable to iterate through json array from $.ajax
I am facing some problem in assigning data to div on my view page. 我在查看页面上将数据分配给div时遇到了一些问题。 The code is something like this:
代码是这样的:
$.ajax({
url: "/api/Flight/SearchFlight",
type: "Post",
data: $('form').serialize() + '&' + $.param({
'TokenId': $("#pageInitCounter").val()
}, true),
success: function(data) {
alert(data);
$('#responsestatus').text(data.Response);
$('#divResult').html(data);
});
Here, as you can see, I can see the whole data in alert box. 如您所见,在这里,我可以在警报框中看到整个数据。 also, I have assigned the whole data to div and i can see whole data perfectly on my page.
另外,我已经将整个数据分配给div,并且可以在页面上完美地看到整个数据。 The Sample Response which i got back is a huge data so i am posting link for it http://pastebin.com/eEE72ySk
我得到的样本响应是一个巨大的数据,因此我为此发布了链接http://pastebin.com/eEE72ySk
Now I want to iterate through each and every data but unable to do it. 现在,我想遍历每个数据,但是无法做到。 Example:
例:
$('#responsestatus').text(data.Response.ResponseStatus);
Then error is: 然后错误是:
UncaughtTypeError:Cannot read property 'ResponseStatus' of undefined
UncaughtTypeError:无法读取未定义的属性“ ResponseStatus”
Please Someone tell me what is wrong here. 请有人告诉我这里有什么问题。 Why cant I iterate over data in response
为什么我不能遍历数据以响应
Use Json datatype..i wrote a sample here.. 使用Json数据类型。.i在这里写了一个示例。
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
You are getting your response back as a string, but trying to operate on it like it were a javascript object. 您正在以字符串形式返回响应,但尝试像对JavaScript对象一样对其进行操作。
There is one of two things you could do 您可以做两件事之一
The first should be as simple as setting the datatype
property on the request 第一个应该像在请求上设置
datatype
属性一样简单
$.ajax({
url: "/api/Flight/SearchFlight",
type: "Post",
datatype: 'json',
data: $('form').serialize() + '&' + $.param({
'TokenId': $("#pageInitCounter").val()
}, true),
success: function(data) {
$('#responsestatus').text(data.Response.ResponseStatus);
});
The second involves parsing the response before using it 第二个涉及使用前解析响应
$.ajax({
url: "/api/Flight/SearchFlight",
type: "Post",
data: $('form').serialize() + '&' + $.param({
'TokenId': $("#pageInitCounter").val()
}, true),
success: function(data) {
var result = JSON.parse(data);
$('#responsestatus').text(result.Response.ResponseStatus);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.