Thursday, May 12, 2016

jquery - Parse JSON with jQuery Example





jquery - Parse JSON with jQuery Example

 $.parseJSON - Takes a well-formed JSON string and returns the resulting JavaScript value.
Example:
        $.post("../_ajaxParts/preplanning/preplanning.php", {
            funct: 'getPrePlanning',
            stakeholder_id:stakeholder_id,
            pad_id:pad_id,
             site:site,          
        }, function(data) {
             formattedJson= $.parseJSON(data);
             formatPrePlanningBox(formattedJson)
        });


 In preplanning.php, we use json_encode to convert to json format.
<?php
 if($_POST['funct'] == "getPrePlanning") {

    $preplanning = new  preplanning();
    $model =  new PreplanningSummaryModel();
    $pad_id =$_POST['pad_id'];
    $total_semester= $model->get_total_semester($pad_id);
    $coursepre_choice= $model->get_coursepre_choice($pad_id);  
    $data = $preplanning->getPrePlanning($_POST);
    $CoursePre = $preplanning->getCoursePre($_POST);
    $arrayForJSON = array("count" => count($data),  "coursepre_choice" => $coursepre_choice, "total_semester" => $total_semester,  "data" => $data, "CoursePre" => $CoursePre);
    echo json_encode($arrayForJSON);
}

?>

No comments:

Post a Comment