JSON and jQuery problem
I am calling a web service and returning the following data in JSON format:
[{"OrderNumber":"12345","CustomerId":"555"}]
In my method of successfully using a web service, I try 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 am getting a bunch of undefined, undefined "lines followed by a valid JSON string. Any idea why? I tried using the method eval()
but it didn't work as I got the error"] ".
a source to share
When you do yours each()
, you are probably getting all the correct values. But right after that, you concatenate data
which is the entire response ( res.d.
) object into a string. I don't understand how this can be the way you want it. Complex objects, such as the ones you can iterate over with $.each()
, can rarely be reasonably represented by strings, simply by concatenating them into an existing string. So this should lead to some bogus data of yours.
Other than that, I think it's pretty confusing that you get errors when you try to evaluate values. I don't think you have to resort to a solution eval
, but you can definitely eval
use your data nonetheless . If you can't, there is something wrong with your answer. If he eval(myVar)
says he expects ]
, then you would like to warn myVar
and give us the full meaning of that.
a source to share
Not sure if I'm missing something, but if you are reporting msg.d, is it undefined? I thought the first parameter of the success callback was the data itself. So it might just be ...
msg.OrderNumber + ", ", + msg.CustomerId;
If it is an array it will be similar to Raji's answer.
$.each(data, function(list, item) {
rtn = rtn + item.OrderNumber + ", " + item.CustomerId + "<br/>";
});
a source to share
Assuming you are using input like
[{"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). don't know why you are using msg.d
- this.CustomerID will return undefined because there is no customer ID in the JSon Response.
a source to share
How ironic it is. I experienced a similar problem a while ago ... It 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);
}
a source to share