0

First of all I want to tell you that I searched and didnot get any solution from stackoverflow's previous post for my problem.So I post my question here.

Following is data returned after echo json_encode($flterarr); {"2":{"surname":"Winchester","mobile":"07898989898","license":"1234567890b"},"3":{"surname":"winchester","mobile":"07898989891","license":"1234567890bytfty"},"4":{"surname":"Test","mobile":"07678543210","license":"23frd"}}

Following is my ajax code

$.ajax(

    { 

        type:"POST",                                    

        url: '../icabz/filterRefresh.php',   

        data:  "filter_reset_surname="+filter_reset_surname+"&filter_reset_mobile="+filter_reset_mobile+"&filter_reset_reg_num="+filter_reset_reg_num+"&filter_reset_approval_status="+filter_reset_approval_status+"&filter_reset_logged_status="+filter_reset_logged_status,
        success: function(data)

        {
            alert(data);
            $.each( data, function(key,val) {

            alert(val);
                /*$.each( val, function( key1, val1 ) {

                alert(val1.surname);
                });*/
            });
        }

    });

when I alert the raw data (without using dataType:"json" or $.parseJSON(data))returned from php page it is showing the data.But when I alert the returned data in the $.each() function it is showing the word and showing following in the console.

TypeError: invalid 'in' operand obj
typeof length === "number" && length > 0 && ( length - 1 ) in obj;

Now if I trying to use dataType:"json" or $.parseJSON(data) and alert the data.then it is showing [object Object]. I am using jQuery 1.11.1 version.

How can I fetch the data to get the surname?

Subhajyoti De
  • 47
  • 1
  • 2
  • 9

2 Answers2

0

you should use dataType:"json" now, try to alert data["2"].surname, you should get "Winchester". if you want to iterate over your object you can do it like:

for (var key in data) { console.log(key, data[key]); }

taken from here

for (var key in data) {
   for (var skey in data[key]) {
        console.log(skey);
   }
}
Community
  • 1
  • 1
ItayB
  • 10,377
  • 9
  • 50
  • 77
  • `for (var key in data) { console.log(key, data[key]); }` I used this and it is showing the key.but I want the value.How can can I get the value as it is multidimensional data returned from JSON. – Subhajyoti De Feb 09 '15 at 07:31
  • Just add another innear for loop (with different var names of course!) – ItayB Feb 09 '15 at 07:35
  • for (var subkey in data[key]) { console.log(subkey) } – ItayB Feb 09 '15 at 07:37
0

Use dataType : "json"

then, try below in your success part:

data= JSON.parse(data);

jQuery.each(data, function(key,val){
    alert(val.surname);
});

http://jsfiddle.net/sachint/pryotLxj/

sachin
  • 76
  • 1
  • 7