首页 > 解决方案 > 在 ionic 中提交数组形式时,JSON 中位置 0 处的意外令牌 <

问题描述

我想通过 api 将数组数据发送到我的数据库,当我单击提交按钮时,我收到此错误异常: EXCEPTION: Unexpected token < in JSON at position 0

我有一个这种数组格式的表格:

<ion-item class="mylist hideme">
      <ion-input type="text"  *ngFor="let post of postList" value="{{post.phoneNo}}" placeholder="Phone" name="phones" #phones></ion-input>
     </ion-item>

     <ion-item class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
        <ion-input type="text" readonly  name="party" placeholder="APC"  value="apc"></ion-input>
        <ion-input type="number" placeholder="Total Votes" name="vote" #vote></ion-input>

    </ion-item>

    <ion-item class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
        <ion-input type="text" readonly name="party"  value="pdp"></ion-input>
      <ion-input type="number" placeholder="Total Votes" name="vote" #vote></ion-input>
       </ion-item>

<ion-item class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
<ion-input type="text" readonly  name="party"  value="apga"></ion-input>  
<ion-input type="number" placeholder="Total Votes" name="vote" #vote></ion-input>
</ion-item>    

在我的 post.ts 中,我尝试以这种方式将其提交到php api

@ViewChild("party") party = [];
@ViewChild("vote") vote = []; 
@ViewChild("phones")phones; 


submit(){
        var headers = new Headers();

          headers.append("Accept", 'application/json');

          headers.append('Content-Type', 'application/json' );

          let options = new RequestOptions({ headers: headers });

          let data = {

            party: this.party = [],
            vote:   this.vote = [], 
            phone:   this.phones.value

          };

          let loader = this.loading.create({

          content: 'Submitting, please wait…',

          });

          loader.present().then(() => {

          this.http.post('http://myschool.com/Api/addResult.php',data, options)

          .map(res => res.json())

          .subscribe(res => {

          loader.dismiss()

          if(res=="RESULT SUCCESSFULLY ENTERED"){

          let alert = this.alertCtrl.create({

          //title:"CONGRATS",

          subTitle:(res),

          buttons: ['OK']

          });

          alert.present();

          this.navCtrl.push(PostPage);

          }else if(res=="YOU ARE NOT AUTHORISE TO SEND RESULT MORE THAN ONCE"){

            let alert = this.alertCtrl.create({

              //title:"ERROR",

              subTitle:(res),

              buttons: ['OK']

              });

              alert.present();

            }else

          {

          let alert = this.alertCtrl.create({

          //title:"ERROR",

          subTitle:(res),

          buttons: ['OK']

          });

          alert.present();

          }

          });

          });

          }

在我的 php

if (isset($data)) {

        $request = json_decode($data);

     $votes = $request->vote;
     $partys =   $request->party; 
     $phones =   $request->phone; 


}
   $phone  = $phones;
    $party = $partys;
    $vote  = $votes; 
    $limit = count($party);
    $date  = date("Y-d-m h:i:s  A",strtotime('+1 hour'));
    $i     = 0; 
    $Scope    = 0;

    $UserAuth = mysqli_query($db,"select * from Master where phoneNo = '$phone'" );
    if ($fetch = mysqli_num_rows($UserAuth)){


     $aso  =  mysqli_fetch_assoc($UserAuth);
     $from =  $aso['phoneNo'];

    $incidentAuth = mysqli_query($db,"select * from res where trim(phoneNo) = '$from'");
    if (!$fetch   = mysqli_num_rows($incidentAuth)){

      $upRup =  "UPDATE Master SET ReportedPU = '1' WHERE trim(phoneNo) = '$from'";
   mysqli_query($db,$upRup);     

      while($i < 6){ 
        $vcast = $vote[$i];
        $pcast = $party[$i];
        if($i+1<= 6){
           $sql    =  "insert into res (phoneNo,category,cNo,date) values ('$from','$pcast','$vcast','$date')";
           $result =  mysqli_query($db,$sql);
        }else{
           $Scope = "200";
        }
        $i++;
      }

      if($Scope == "200"){
        $response =  'RESULT NOT SUCCESSFULLY ENTERED. PLEASE TRY AGAIN ' .$db->err;
      }else{
        $response = 'RESULT SUCCESSFULLY ENTERED';
      }

    }else{
     $response = 'YOU ARE NOT AUTHORISE TO SEND RESULT MORE THAN ONCE';
    }
    }else{
        $response = "THIS IS AN UNCONFIRMED AGENT";
    }

echo json_encode( $response);

$db->close();

我已经尝试了一切可能但无济于事。如何提交数组表单并将其传递给我的 php?

现在我卡住的地方是如何提交 ionic 中的数组值。

谢谢。

标签: phparraysionic-frameworkmysqliionic2

解决方案


推荐阅读