Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json undefined error

I have action to edit then send json data

<?php
    public function btneditAction() {
    $data['id'] = $this->request->getPost('id');
    $data['uname'] = $this->request->getPost('uname');
    $data['basesalary'] = $this->request->getPost('basesalary');
    $data['travelfee'] = $this->request->getPost('travelfee');
    $data['overtime'] = $this->request->getPost('overtime');
    $data['ssc_emp'] = $this->request->getPost('ssc_emp');
    $data['ssc_comp'] = $this->request->getPost('ssc_comp');
    $Salarydetail = new SalaryMaster();
    $pont=$Salarydetail->btnedit($data);
    echo json_encode($pont);
    $this->view->disable();
}
?>

It shows in network is like this

{"valid":true} //when true {"valid":false}   //when false

I want to that valid value ,but when i alert ,it shows undefined only?

BtnEdit : function(val){
    var form=$('#edit_salary');
    $.ajax({
        type:'POST',
        data: form.serialize(),
        dataType:'json',
        url : "btnedit",
        success:function(d){
            alert(d.valid);      //undefined
            alert(d);            //{"valid":true}                     
        }
    });
}
like image 530
David Jaw Hpan Avatar asked Jan 08 '23 18:01

David Jaw Hpan


2 Answers

Use JSON.parse to transform the string in an Object:

success:function(d){
    d = JSON.parse(d);
    alert(d.valid);      // true
}
like image 150
michelem Avatar answered Jan 15 '23 14:01

michelem


You need to use JSON.parse to transform the string in an Object:

Try

success:function(d){
    d = JSON.parse(d);
    alert(d.valid);
}
like image 31
Hassaan Avatar answered Jan 15 '23 15:01

Hassaan