0

I have an AJAX call:

$.ajax({
    type     : "POST",
    dataType : 'JSON',
    url      : "../hotelroomtype",
    data     : form_data,
    success  : function(data){
        var datas = $.parseJSON(data);
        alert(datas['categorys'].categorytype0)
        // var returnedData = $.parseJSON(data);
        // alert(returnedData);
        // $('.x').html(returnedData);
        //
        // for(i=1;i<oldSeason;i++){
        //     $('#roomcategory'+i).html(data.categorytype+i);
        // }
    }
});

Server returned to my js the following JSON data

[{"categorytype0":"<div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div>"},{"categorytype1":"<div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div><div class='panel-body'>adfdfs<\/div>"}]

When I check with alert it only shows [object object]. Where is the problem?

Edited: AJAX call:

 $.ajax({
        type     : "POST",
        url      : "../hotelroomtype",
        data     : form_data,
        success  : function(data){
          dataType : 'JSON',

             for(i=1;i<oldSeason;i++){
                 $('#roomcategory'+i).html(data.categorytype+i);
             }
        }
    });

here i get the result, but not in proper way.I need the result in categorytype wise. but it is in array..i can't get it separatly

here is my json call

$data = array();
for($i=0;$i<$cnt;$i++)
{
    array_push($data, array('categorytype'.$i.'' =>$roomtype));
}
    echo json_encode($data, JSON_UNESCAPED_SLASHES);
MaRKR
  • 47
  • 1
  • 11

3 Answers3

0

Try replacing

alert(datas['categorys'].categorytype0)

with

alert(datas.categorytype0)

Paddyd
  • 1,870
  • 16
  • 26
0

I think that you don't need to do the parseJSON function since you are working with Javascript and therefore data is already a JSON object.
You can access directly to each of the objects like: data[x]. Then you can access each of the properties of the element lika an associative array.

dbonfill
  • 34
  • 4
0

You've set the dataType to 'json', so the parameter should already be a json object.

Also, your json is an array containing a single object. It has no element named 'categorys'.

I recommend, instead of using alert, using console.log. That will show you the whole object in the console, in a much more useful format.

Chris Lear
  • 6,592
  • 1
  • 18
  • 26