tags:

views:

3078

answers:

4

I couldn't find anything about that.

Here is the Json returned: {"Email":"Please enter your Email.","Password":"Please enter a password."}

Here is my code:

$(function() {
 $("#btnSubmit").click(function() {
  $.ajax({
   url: "/account/signup",
   type: "POST",
   dataType: "json",
   data: { 
    Email: $("#strEmail").val(),
    Password: $("#strPassword").val()
   },
   success: function(j) {
           $(".errMsg").hide();
           alert(j.length); // I couldn't get the total count
    $.each(j, function(n) {
     $("#err" + n).html(j[n]);
     $("#err" + n).show();
    })

   },
   error: function(req, status, error) {
    alert(req);
   }
  });
 });
});
+3  A: 

If you have something like this:

var json = [ {a:b, c:d}, {e:f, g:h, ...}, {..}, ... ]

then, you can do:

alert(json.length)
Tiago
Thanks for your answer, but I tried your code, but i doesn't work for me.
This definitely helped me out, thanks!
Nicholas Kreidberg
A: 

Your json isn't an array, it hasn't length property. You must change your data return or the way you get your data count.

tanathos
Even I changed the Json result (I'm sure it's correct), the length still doesn't work.
+2  A: 

Why would you want length in this case?

If you do want to check for length, have the server return a JSON array with key-value pairs like this:

[
  {key:value},
  {key:value}
]

In JSON, [ and ] represents an array (with a length property), { and } represents a object (without a length property). You can iterate through the members of a object, but you will get functions as well, making a length check of the numbers of members useless except for iterating over them.

svinto
A: 

What you're looking for is

j.d.length

The d is the key. At least it is in my case, I'm using a .NET webservice.

 $.ajax({
            type: "POST",
            url: "CantTellU.asmx",
            data: "{'userID' : " + parseInt($.query.get('ID')) + " }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg, status) {
                ApplyTemplate(msg);
                alert(msg.d.length);
            }
        });
Frenchie
Don't forget to mark it as answer if it works for you.
Frenchie