0

I'm trying to pass a variable from the controller to the view through JSON. The data that I want to check is available but if I try to access it like "data.status" it comes back as undefined.

The code that I use in the controller is:

public function checkserie(){
    $data['status'] = 'success';
    $this->output->set_output(json_encode($data, JSON_PRETTY_PRINT));
}

And the code that I use in the view is:

$.post('<?php echo base_url("index.php/manageorderseries/checkserie")?>', { name: $("#seriename").val()}).done(function(data) {
        alert(data.status);
});

I also saw that when I replace "data.status" with just "data", I receive the following in my alert:

{status: "success"}

What did I miss to access the variable as "data.status"?

Thanks in advance ! Kind regards,

Jonas Vandevelde

1 Answer 1

1

The response you are getting is JSON, so JS needs to parse that if you want to use it like an ordinary JS object.

You can use either JSON.parse() or $.parseJSON() (since you already are using jQuery) to accomplish this.

$.post('<?php echo base_url("index.php/manageorderseries/checkserie")?>', { name: $("#seriename").val()}).done(function(data) {
    var dataObj = JSON.parse(data);
    alert(dataObj.status);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.