1

I am trying to perform operations on server by using AJAX

jQuery(document).on('click','a.edit', function (e) {
            var id=$(this).prop('id');
            var params="id="+id;
            $.ajax({
                    data:params,
                    method: 'post',
                    url:base_url+'ajax/GetCategoryDetails/',
                    beforeSend: function(){ 
                    //alert(1)
                    },
                    complete: function(){  },
                    success: function(resultSet)
                    {
                        console.log( typeof resultSet );
                        console.log( resultSet );
                        console.log(resultSet.responseJSON.SUCCESS);
                        var result = jQuery.parseJSON(resultSet.responseJSON )
                        //alert(resultSet.SUCCESS);
                        console.log(resultSet.responseJSON );

                        //alert(result);
                        //App.stopPageLoading();
                        if(resultSet.ERROR)
                        {
                            alert(2);
                        }
                        else if(resultSet.SUCCESS)
                        {

                            alert(resultSet.DATA.name);
                            //$('#category').html(x.name);


                        }
                    }
                });


    });

and then on the server side after performing operation i have to send back some response and for that i have made an array called resultset and i am sending it back like this

public function GetCategoryDetails()
{
    if($_POST)
    {
        $this->load->model('category');
        $category=array('id'=>$this->input->post('id'));
        $cat=$this->category->getCategoryDetails($category);
        if($cat)
        {
            $resultSet['SUCCESS'] = 1;
            $resultSet['DATA'] = $cat;
        }
        else
        {
            $resultSet['ERROR'] = array('1');
            $resultSet['MESSAGE'] = array('Could not get Category Details. Try Later');
        }
        //var_dump( json_encode($resultSet));exit;
        echo json_encode($resultSet);
    }

}

it looks like the following in var dump:

string '{"SUCCESS":1,"DATA":{"id":"31","name":"asdasdasd"}}' (length=51)

now after removing the vardump, when it returns back to the ajax the resultSet looks like the following in

alert(resultSet);

resultSet data

after using the jQuery.parseJSON(resultSet); it returns [object Object] in the alert The JSON is not being parsed or what can be the problem?

Art
  • 141
  • 1
  • 10
  • 1
    your data is already json. you can simply access the `SUCCESS` key and the `DATA` key. – MMK Jan 17 '16 at 18:26
  • 1
    alert() will always show [object Object]. Use console.log() instead and see the result in console F-12 – Ahsan Jan 17 '16 at 18:29
  • i guess that would be resultSet.data.name – MMK Jan 17 '16 at 18:30
  • `result` should be `result = jQuery.parseJSON( resultSet.responseJSON )`; – MMK Jan 17 '16 at 18:31
  • `console.log(resultSet.SUCCESS);` results in undefined and same is the `case with `console.log(resultSet.DATA);` – Art Jan 17 '16 at 18:36
  • my dear friend, try `console.log(resultSet.responseJSON.SUCCESS);` – MMK Jan 17 '16 at 18:38
  • `console.log(resultSet.responseJSON );` results undefined – Art Jan 17 '16 at 18:40
  • `console.log(resultSet.responseJSON.SUCCESS);` says cannot read property SUCCESS for undefined – Art Jan 17 '16 at 18:41
  • try console.log( resultSet ) and let us know what you see – MMK Jan 17 '16 at 18:44
  • What exactly is the specific problem? After parsing JSON you should have [object,object] in alert. Using alert is not the right way to inspect objects ...use `console.log(variable)` – charlietfl Jan 17 '16 at 19:16
  • 2
    also note that setting `dataType:'json'` and/or setting proper content type header at server will tell jQuery to parse the json for you – charlietfl Jan 17 '16 at 19:41
  • @charlietfl +1 for that ! @Art try `header('Content-Type: application/json');` before echoing the json. and here is a post link http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script" about that. – MMK Jan 17 '16 at 19:43
  • If that's what this whole question was about ... *why isn't it already parsed?* ...it sure isn't clear in question itself – charlietfl Jan 17 '16 at 19:45

2 Answers2

0

Try console.log(resultSet.responseJSON.SUCCESS); if you have a response from the server you will have a responseJSON key within that response.

so if you try to access resultSet.SUCCESS you will get undefined because SUCCESS is not a key of resultSet but a key of resultSet.responseJSON.

So your data is within resultSet.responseJSON.DATA.

MMK
  • 609
  • 5
  • 16
  • `console.log(resultSet` results `{"SUCCESS":1,"DATA":{"id":"31","name":"asdasdasd"}}` while `console.log(resultSet.responseJSON.SUCCESS);` results `Uncaught TypeError: Cannot read property 'SUCCESS' of undefined` which means `resultSet.responseJSON` is `null` – Art Jan 17 '16 at 18:50
  • i recommend that you try to know the type of the resultSet. try `console.log( typeof resultSet )` – MMK Jan 17 '16 at 18:56
  • it is a `string` i just checked – Art Jan 17 '16 at 19:03
  • i parsed the your string in the console. i did var a= `{"SUCCESS":1,"DATA":{"id":"31","name":"asdasdasd"}}` and then i did `v = $.parseJSON( a );` and i can access the `id` and `name` by doing ` v.DATA.id` and `v.DATA.name` respectively. so you should be able to do the same in your script. – MMK Jan 17 '16 at 19:12
  • @MMK `$==jQuery` ..last comment makes no sense – charlietfl Jan 17 '16 at 19:14
  • @charlietfl you got any suggestion on what's wrong in Art's problem? – MMK Jan 17 '16 at 19:18
  • I don't understand the problem at all to be honest – charlietfl Jan 17 '16 at 19:18
  • @charlietfl well my dear friend i guess that Art is not being able to access the `DATA` key in the parsed json string. see if you can help – MMK Jan 17 '16 at 19:23
  • @Art if you could post your entire php and javascript file it would be easy for us to debug – MMK Jan 17 '16 at 19:24
  • @MMK can only help when problem is understandable...guessing is a waste of time and often goes down completely the wrong path – charlietfl Jan 17 '16 at 19:26
  • how can i give you files? – Art Jan 17 '16 at 19:30
  • 1
    @charlietfl, yeps totally agree with that. no offense but the question states `after using the jQuery.parseJSON(resultSet); it returns [object Object] in the alert The JSON is not being parsed or what can be the problem?` i think that is pretty much understandable. so by that i think the problem lies either in the data received or when the data is being parsed. – MMK Jan 17 '16 at 19:35
  • @MMK well my inerpretation is that OP doesn't understand why it is `[object,object]` or how to work with objects. The alert is correct – charlietfl Jan 17 '16 at 19:37
  • @Art fine to agree but totally unclear where your actual problems are – charlietfl Jan 17 '16 at 19:38
0
success: function(resultSet)
                {
                    console.log( typeof resultSet );
                    console.log( resultSet );
                    console.log(resultSet.responseJSON.SUCCESS);
                    var result = jQuery.parseJSON(resultSet.responseJSON )
                    //alert(resultSet.SUCCESS);
                    console.log(resultSet.responseJSON );

                    //alert(result);
                    //App.stopPageLoading();
                    if(resultSet.ERROR)
                    {
                        alert(2);
                    }
                    else if(resultSet.SUCCESS)
                    {

                        alert(resultSet.DATA.name);
                        //$('#category').html(x.name);


                    }
                }

The mistake i made was that i parsed the object $resultSet and stored it in$result and still used resultSet.ERROR, resultSet.DATA.name, and resultSet.SUCCESS , rather it should be accessed like result.ERROR, result.DATA.name, result.SUCCESS

Art
  • 141
  • 1
  • 10