Issue with JSON and jQuery
I'm calling a web service and returning the following data in JSON format:
[{"OrderNumber":"12345","CustomerId":"555"}]
In my web service success method, I'm trying to parse both:
$.ajax({
type: "POST",
url: "MyService.asmx/ServiceName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var data = msg.d;
var rtn = "";
$.each(data, function(list) {
rtn = rtn + this.OrderNumber + ", " + this.CustomerId + "<br/>";
});
rtn = rtn + "<br/>" + data;
$("#test").html(rtn);
}
});
but I'm getting a bunch of "undefined, undefined" rows followed by the correct JSON string. Any idea why? I've tried using the eval()
method but that didn'开发者_开发问答t help as I got some error message talking about ']' being expected.
When you're doing your each()
you're probably getting all the right values. But immediately after that, you're concatenating data
, which is the entire response object (res.d.
), into the string. I don't see how that could ever be what you want. Complex objects such as those that you can iterate over with $.each()
can rarely be sensibly string-represented by simply concatenating them into an existing string. So that ought to cause some of your bogus data.
Other than that, I think it's rather disconcerting that you're getting errors when you're trying to eval the values. I don't think you should ever need to resort to an eval
solution, but nevertheless, you should definitely be able to eval
your data. If you can't, there's something badly malformed in your response. If eval(myVar)
says it's expecting an ]
, then you would want to alert myVar
and give us the full value of that.
I might be wrong but could it be this:
rtn = rtn + list.OrderNumber + ", " + list.CustomerId + "<br/>";
HTH
Not sure if I am missing something but if you alert msg.d is the value undefined? I thought the first parameter of the success callback was the data itself. So it could just be...
msg.OrderNumber + ", ", + msg.CustomerId;
If it is an array then it would be like Raja's answer.
$.each(data, function(list, item) {
rtn = rtn + item.OrderNumber + ", " + item.CustomerId + "<br/>";
});
With the assumption that you are using the input as
[{"StreetAddress1":"123 Main St","BusinessName":"ABC Inc","OrderNumber":"987654"},{"StreetAddress1":"75 Main St","BusinessName":"Google","OrderNumber":"654321"},{"StreetAddress1":"27 Main St","BusinessName":"Microsoft","OrderNumber":"123456"}]
- eval(msg) should be used. dont know as to why you use msg.d
- this.CustomerID will return undefined, because there is no customerID in the JSon Response
I finally got it working by doing this:
var data = eval('(' + msg.d + ')');
I'm sure this isn't great, but it works. If anyone can provide a reason why this works and/or a solution to my issue, I'd still appreciate it. :)
How ironic is this.. I experienced similiar problem some time back... This worked in my case.
function BuildTable(msg) {
var table = '<table><thead><tr><th>First Name</th><th>Middle Name</th><th>Last Name</th></thead><tbody>';
for (var i = 0, l = msg.length; i < l; i++)
{
var person = msg[i];
var row = '<tr>';
row += '<td>' + person.FirstName + '</td>';
row += '<td>' + person.MiddleName + '</td>';
row += '<td>' + person.LastName + '</td>';
row += '</tr>';
table += row;
}
table += '</tbody></table>';
$('#Container').html(table);
}
link text
精彩评论