views:

110

answers:

6

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.

A: 

I might be wrong but could it be this:

 rtn = rtn + list.OrderNumber + ", " + list.CustomerId + "<br/>";

HTH

Raja
Nope. Already tried that.
Jason N. Gaylord
A: 

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/>";
});
Chris Gutierrez
It shows the correct JSON string when I do an alert. Also, it is an array. I've switched it like Raja has mentioned and it doesn't help (yet).
Jason N. Gaylord
Have you also tried using the second parameter in the $.each callback? The first is the index, the second is the value. See my edit above....
Chris Gutierrez
I get OrderNumber is null or not an object.
Jason N. Gaylord
Here's the value of my current JSON response: [{"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"}]
Jason N. Gaylord
+1  A: 

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.

David Hedlund
Here's the value of my current response:[{"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"}]This is being rendered by using ASP.NET 3.5's JavaScriptSerializer().Serialize() method.
Jason N. Gaylord
I do agree that this line rtn = rtn + "<br/>" + data; is a bit strange.
Chris Gutierrez
If I take out that line, same error.
Jason N. Gaylord
@Jayson N. Gaylord: there are a few things here that disturb me. first of all, if i `eval` a string with the value you've posted, i don't get the error that you've describe, so something at some point does not have the value that you think it has. second, do you manually invoke the `JavaScriptSerializer`? because if it's a web method, as i expect it is, what you want to do is to return a .NET object, and .NET will automatically serialize it (but perhaps that's what you meant)
David Hedlund
I did end up using eval but on the msg.d and not on just msg.
Jason N. Gaylord
A: 

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"}]

  1. eval(msg) should be used. dont know as to why you use msg.d
  2. this.CustomerID will return undefined, because there is no customerID in the JSon Response
Sundararajan S
That's because I changed the array to eliminate the customerID info and the other values.I did switch it to eval.
Jason N. Gaylord
A: 

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. :)

Jason N. Gaylord
if that works, but not `data = msg.d`, then `msg.d` is a string and not an object. Verify this by `alert(typeof msg.d)`. if that alerts `string` and not `object` it explains everything. in that case, i'll hazard that you're doing something wrong at the server side (well yes, same guess as in my most recent comment to my own answer). i'm guessing your webmethod looks like this `public string Abc() { ... return JavaScriptSerializer.Serialize(myObject); }`, instead, if you change it to `public object Abc() { ... return myObject; }`, your `msg.d` should be a javascript object and not a string.
David Hedlund
That makes sense David. I thought you did have to serialize the object when returning it though?
Jason N. Gaylord
@Jason N. Gaylord: nah, if you return it as an object it'll be serialized automatically. (and *deserialized* automatically server-side). that's why what you're doing ends up with an object serialized twice over, so that when you receive `msg.d` automatically deserialized (once) it's still a string =)
David Hedlund
A: 

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

Steven