4

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}                     
        }
    });
}
Community
  • 1
  • 1
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52

3 Answers3

7

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

success:function(d){
    d = JSON.parse(d);
    alert(d.valid);      // true
}
michelem
  • 14,430
  • 5
  • 50
  • 66
1

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

Try

success:function(d){
    d = JSON.parse(d);
    alert(d.valid);
}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
1

As of you are using Phalcon framework, I would recommend you to use it's built-in abilities to properly handle json responses:

$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent($responseAsArray);
$this->response->send();

If header of application/json is included in response, jQuery starts to handle it properly and you do not need to JSON.parse your frame.

For handling json responses in API module in one of my applications I have a ControllerBase that extends \Phalcon\Mvc\Controller with this event handler:

use \Phalcon\Mvc\Controller;

class ControllerBase extends Controller {

/**
 * Captures method result and tries to make a JSON response out of it.
 * 
 * @param \Phalcon\Mvc\Dispatcher $dispatcher
 * @return \Phalcon\Http\Response
 */
protected function afterExecuteRoute($dispatcher) {

    $content = $dispatcher->getReturnedValue();

    if(is_object($content)) {
        if(is_callable(array($content, 'toArray'))) {
            $content = $content->toArray();
        } else {
            $content = (array) $content;
        }
    }

    $frame = $this->getFrame($content, $dispatcher); // protocol frame creation helper

    $this->response->setContentType('application/json', 'UTF-8');
    switch($frame['code']) {
        case 200:
            $this->response->setStatusCode(200, 'OK');
            break;
        case 400: 
            $this->response->setStatusCode(404, 'Not found');
            break;
        case 500: 
            $this->response->setStatusCode(503, 'Service Unavailable');
            break;
    }

    // clearing potential warnings
    $ob = ob_get_clean();
    if(strlen($ob) > 0) {
        /**
         * @todo some logging of $ob !
         * this will be a dead code if you will make an Error2Exception handler
         */
        echo($ob); die();
    }

    // settinf response content as JSON
    $this->response->setJsonContent($frame);

    return $this->response->send();
}
}
yergo
  • 4,761
  • 2
  • 19
  • 41