Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70747

ERROR SyntaxError: Unexpected token S in JSON at position 0?

$
0
0

@kmsundar wrote:

I used PHP to post the code in API. I don’t know how to get data in ts file using subscribe (data). But in console network I have write output in JSON format.

home.ts

workerdailyreport(){
    return new Promise((reslove) => {
        let body = {
            aksi: 'update_workerdailyreport',
            full_nuts: this.x,
            half_nuts: this.y,
            rejected_nuts: this.z,
            id: this.id,
        };
        this.postPvdr.postData(body, 'proses-api.php').subscribe(data => {
            console.log(data);
            for (let passdata of data.result) {
                this.passdata.push(passdata);
            }
                reslove(true);
        });
    });
}

strong text

postData(body, file) { 
    let type = "application/json; charset=UTF-8"; 
    let headers = new Headers({ 'Content-Type': type }); 
    let options = new RequestOptions({ headers: headers }); 

    return this.http.post(
        this.server + file, 
        JSON.stringify(body),
        options
    ).map(res => res.json()); 
}

proses-api.php

if($postjson['aksi']=='update_workerdailyreport'){
    $data= array();
    $job_id = $postjson['id'];
    $batchkpr = 0;
    $tempwight = 0;
    $batchNmae= 0;
    $name = '';
    $select_job="SELECT * FROM invoice WHERE job_id=".$job_id;
    $querySelectjob = mysqli_query($mysqli,$select_job);

    while($row = mysqli_fetch_array($querySelectjob)){ 
        //check batch KPR is empty
        $select_batch="SELECT * FROM batch WHERE batch_id='".$row['batch_id']."' AND deleted_status='0'";

        $querysselect_batch = mysqli_query($mysqli,$select_batch); 

        while($fletchbatch = mysqli_fetch_assoc($querysselect_batch)){ 
            $batchkpr= $fletchbatch['kpr'];
            $workerbatch= $fletchbatch['created_at'];
            $batchNmae= date("d-m-Y",strtotime($workerbatch));
        }
        //select user name
        echo $select_name="SELECT * FROM user_table WHERE user_id='".$row['user_id']."' AND deleted_at='0'";
        $querysselect_name = mysqli_query($mysqli,$select_name); 
        echo $count =mysqli_num_rows($querysselect_name);
        while($fletchname = mysqli_fetch_assoc($querysselect_name)){ 
            $name= $fletchname['f_name'].' ' .$fletchname['l_name'];
        }

        if($batchkpr == "0.00"){
            echo $result = json_encode(array('success'=>false, 'msg'=>'! First Update KPR in Batch'));
        }
        else{  
            $weight= 3 * $row['weight']; 
            $full_nuts=$postjson['full_nuts'];
            $half_nuts=$postjson['half_nuts']; 
            $rejected_nuts=$postjson['reject_nuts']; 
            $shell= $weight-($full_nuts+$half_nuts+$rejected_nuts);
            $newwight= $tempwight-$weight;
            // calculate KPR 
            $kpr=(($full_nuts+$half_nuts)/$weight)*100;
            $data[] = array(
                'name' => $name,
                'batchname' => $batchNmae,
                'weight' => $row['weight'],
                'full_nuts' => $full_nuts,
                'half_nuts'=> $half_nuts,
                'rejected_nuts'=> $rejected_nuts,
                'shell'=>$shell,
                'kpr'=>$kpr,
            );
        }
    }

    if($querySelectjob) $result = json_encode(array('success'=>true, 'result'=>$data));
    else $result = json_encode(array('success'=>false));

    echo $result;
}

Error I got error in console :
Capture

but I got the correct data in network API file

{"success":true,"result":[{"name":"SASI A","batchname":"02-11-2019","weight":"10","full_nuts":"5","half_nuts":"3","rejected_nuts":"1","shell":21,"kpr":26.666666666666668}]}

I don’t no where a did the mistake. please help :slight_smile:

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70747

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>