简体   繁体   中英

Cannot Parse JSON data With jQuery.parseJSON

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数据

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM