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

+2


a source to share


6 answers


I finally got this by doing this:

var data = eval('(' + msg.d + ')');

      



I'm sure it's not great, but it works. If anyone can explain the reason why this works and / or the solution to my problem, I'd appreciate it anyway. :)

0


a source


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.

+1


a source


I may be wrong, but could it be like this:

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

      

NTN

0


a source


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/>";
});

      

0


a source


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.
0


a source


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);
}

      

link text

0


a source







All Articles