0

Hi I have following ajax function..

         $.ajax({
                url: './sales/cat/salesCat'
              , target: null
              , type: "get"
              , dataType: "json"
              , data: {

                  locale: LOCALE
              }
              , success: function (object) {
                  $.each(object, function (key, value) {
                      alert(key + ":" + value);
                  })
              }
         });

It returns following output

 {
  "success": true,
  "totalCount": 2,
  "data": [
    {
      "East Division": "Yearly Sales"
      "West Division": "Yearly Sales"
    }
  ]
}

Problem is I am trying to read the returned values to use it. When Success function executes and in my alert I get alert message

         data:[object Object]

Please let me know how to correct the

          $.each(object, function (key, value) {
                  alert(key + ":" + value);
              })

So alert can display for example East Division:Yearly Sales Thanks for your help.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
J. Davidson
  • 3,297
  • 13
  • 54
  • 102
  • 2
    Don't use `alert` to debug code, use `console.log`. `[object Object]` is a stringified object, as `alert` only takes strings. – elclanrs Jan 13 '14 at 22:33
  • possible duplicate of [jQuery - How to recursively loop over an object's nested properties?](http://stackoverflow.com/questions/12295494/jquery-how-to-recursively-loop-over-an-objects-nested-properties) – Blazemonger Jan 13 '14 at 22:33
  • first of all you missed one comma, second this happens because it gets stuck at data array. – Alex Shilman Jan 13 '14 at 22:35
  • @Blazeonger! I already searched the Stackoverflow this is not what my question is. It is not a duplicate post. I guess if you want to far fetch you can say anything is duplicate. – J. Davidson Jan 13 '14 at 22:39

3 Answers3

1

The data you're getting back is an object, hence your data:[object Object] response in the alert.

To debug, use console.log() instead and look in your browser console (Chrome: F12 debug tools, for example).

If you wanted to get the first value of your data object in this case, you can reference it one of two ways:

data["East Division"]

Or

data[0]

You can also use recursion to do this, see how to Loop through an array in JavaScript

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • I think he wants a way to recursively iterate over object, not to reference each one. +1 for you support – Fred Jan 13 '14 at 22:37
0

That's because when using alert() to output an object the object gets converted to a string. An object as a string is "[object Object]".

Instead you should use console.log() to output the object to your browser's JavaScript console.

var data = { key: "value" };
data.toString(); // "[object Object]"
data; // Object { key: "value" }

The other problem you have is that your data response is missing a , and is thus invalid:

"data": [
    {
      "East Division": "Yearly Sales" // <--- No comma
      "West Division": "Yearly Sales"
    }
]

This will throw:

SyntaxError: Unexpected token :

Community
  • 1
  • 1
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

You can translate it to a JSON string (for debugging obviously):

alert(JSON.stringify(object));
Jochem Kuijpers
  • 1,770
  • 3
  • 17
  • 34