我在Ionic下面写了代码,它给出了我的错误如下

时间:2017-07-26 12:23:37

标签: php mysql ionic-framework

我正在使用mysql处理Ionic app。错误显示如下:

ERROR SyntaxError:位于0的JSON中的意外标记S

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import { AddTechnology } from "../add-technology/add-technology";
import 'rxjs/add/operator/map';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

   public items : any = [];
   constructor(public navCtrl: NavController,
               public http   : Http)
   {

   }


   ionViewWillEnter()
   {
      this.load();
   }

   // Retrieve the JSON encoded data from the remote server
   // Using Angular's Http class and an Observable - then
   // assign this to the items array for rendering to the HTML template  //https://go-general.000webhostapp.com/technologies/retrieve-data.php
   load()
   {
      this.http.get('https://go-general.000webhostapp.com/technologies/retrieve-data.php')
      .map(res => res.json())
      .subscribe(data =>
      {
         this.items = data;
      });
   }


   // Allow navigation to the AddTechnology page for creating a new entry
   addEntry()
   {
      this.navCtrl.push(AddTechnology);
   }


   // Allow navigation to the AddTechnology page for amending an existing entry
   // (We supply the actual record to be amended, as this method's parameter,
   // to the AddTechnology page
   viewEntry(param)
   {
      this.navCtrl.push(AddTechnology, param);
   }


}

retrieve.php

  <?php

    header('Access-Control-Allow-Origin: *');

    //Define database connection parameters
    $hn = 'localhost';
    $un = 'root';
    $pwd = 'yashbaxi';
    $db = 'technologies';
    $cs = 'utf8';

    //Set up the PDO parameters
    $dsn = "mysql:host=" . $hn . ";port=3306;dbname=" . $db . ";charset=" . $cs;
    $opt = array(
                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE  => PDO::FETCH_OBJ,
                PDO::ATTR_EMULATE_PREPARES   => false
            );

    //Create a PDO instance (connect to the database)
    $pdo = new PDO($dsn, $un, $pwd, $opt);
    $data = array();

    //Attempt to query database table and retrieve data
    try{
        $stmt = $pdo->query('SELECT id, name, description FROM technologies ORDER BY name ASC');
        while($row = $stmt->fetch(FETCH_OBJ)){
            //Assign each row of data to associative array
            $data[] = $row;
        }

        //Return data as JSON
        echo json_encode($data);
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }

?>

它不会在从mysql数据库中检索到的屏幕上显示结果

代码没有错误,但我不明白为什么会发生这种错误。

请帮助我。

0 个答案:

没有答案